|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-09-11 19:44 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Dec 05 14:00:02 2025 UTC |
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>"; } ?>