#42812 [Opn->Bgs]: integer value returned by php incorrect
| From: | pajoye@php.net | Date: | Mon, 01 Oct 2007 10:38:27 +0000 |
| Subject: | #42812 [Opn->Bgs]: integer value returned by php incorrect | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-118008@lists.php.net to get a copy of this message | ||
ID: 42812
Updated by: pajoye@php.net
Reported By: laurent at polenord dot com
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: linux
PHP Version: 5.2.4
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.
NB: remove the (int) cast to see what actually happens.
Previous Comments:
------------------------------------------------------------------------
[2007-10-01 10:33:30] laurent at polenord dot com
Description:
------------
infact the problem occurs on PHP 5.2.3
I want to simply return a float multiplied by a million, and force it
to an integer value to avoid php returns int in weird format (2.9E+06
for example), and it doesn't return the correct value.
This is very strange as it seems to occurs only on the '2.09' value :
it's ok with 1.09 and 3.09, 2.19, 2.091 and 2.089, etc...
Reproduce code:
---------------
$cpc = '2.09';
echo $cpc * 1000000; // returns correct value : 2090000
echo "<br/>";
echo (int)($cpc * 1000000); // returns 2089999 !!
echo "<br/>";
echo (int)($cpc * 1000 * 1000); // return correct value.
Expected result:
----------------
2090000
2090000
2090000
Actual result:
--------------
2090000
2089999
2090000
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=42812&edit=1