#50398 [Opn->Bgs]: convert float to int causes an error
ID: 50398
Updated by: johannes@php.net
Reported By: blanca at netvision dot net dot il
-Status: Open
+Status: Bogus
Bug Type: Unknown/Other Function
Operating System: windows xp
PHP Version: 5.3.1
New Comment:
Floating point values have a limited precision. Hence a value might
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly
printing it without any mathematical operations.
If you would like to know more about "floats" and what IEEE
754 is, read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
Thank you for your interest in PHP.
The docs say
"When converting from float to integer, the number will be rounded
towards zero."
http://php.net/manual/en/language.types.integer.php#language.types.integer.casting
Now combining this with the unprecise nature of floating point numbers
this gives "random" results.
Previous Comments:
------------------------------------------------------------------------
[2009-12-07 12:40:31] blanca at netvision dot net dot il
Description:
------------
when converting 12.999999999999999 from float to int,
the output is 12
but, when converting 12.9999999999999999 from float to int,
the output is 13
Reproduce code:
---------------
$a=12.9999999999999999;
$b=(int)$a;
$a=12.999999999999999;
$c=(int)$a;
$str="\$b is: ".$b." and \$c is: ".$c;
echo $str;
Expected result:
----------------
$b is: 12 and $c is: 12
Actual result:
--------------
$b is: 13 and $c is: 12
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=50398&edit=1
Thread (2 messages)