#42628 [Opn->Bgs]: WRONG ARITMETHIC RESULT
| From: | pajoye@php.net | Date: | Tue, 11 Sep 2007 20:49:50 +0000 |
| Subject: | #42628 [Opn->Bgs]: WRONG ARITMETHIC RESULT | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-117415@lists.php.net to get a copy of this message | ||
ID: 42628
Updated by: pajoye@php.net
Reported By: jortac2002 at yahoo dot com dot mx
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: WINDOWS XP PROFESSIONAL
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.
Previous Comments:
------------------------------------------------------------------------
[2007-09-11 20:36:02] admin at ifyouwantblood dot de
This is the thing with floating numbers, just as the manual states:
It is quite usual that simple decimal fractions like 0.1 or 0.7
cannot be converted into their internal binary counterparts without a
little loss of precision.
The follwing works as you would expect it therefore:
<?
$tree = 366.22;
$one = 1519.36;
$two = $one - $tree;
$four = $two + $tree;
$difer = $one - $four;
echo "one " . $one . " equals four " . $four . "<br>";
if ($four == $one) {
echo "THEY ARE EQUAL <br>";
}
else {
echo "THEY ARE NOT EQUAL DIFFERENCE " . $difer . "<br>";
}
?>
------------------------------------------------------------------------
[2007-09-11 19:44:27] jortac2002 at yahoo dot com dot mx
Description:
------------
I have the following code :
It is very simple code, and the result is very stange because
amounts are the same but result shows "NOT EQUAL"
Reproduce code:
---------------
<html>
<body>
<?
$one = 1519.36;
$two = 1153.14;
$tree = 366.22;
$four = $two + $tree;
$difer = $one - $four;
echo "one " . $one . " equals four " . $four . "<br>";
if ($four == $one) {
echo "THEY ARE EQUAL <br>";
}
else {
echo "THEY ARE NOT EQUAL DIFFERENCE " . $difer . "<br>";
}
?>
</body>
</html>
Expected result:
----------------
THEY ARE EQUAL
Actual result:
--------------
THEY ARE NOT EQUAL DIFFERENCE -2.737367544323E-013
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=42628&edit=1