Bug #32082 [Com]: Comparing of floating point values fail
| From: | jbertron at spotxchange dot com | Date: | Wed, 24 Jul 2013 16:21:04 +0000 |
| Subject: | Bug #32082 [Com]: Comparing of floating point values fail | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-180468@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=32082&edit=1
ID: 32082
Comment by: jbertron at spotxchange dot com
Reported by: weber at mapsolute dot com
Summary: Comparing of floating point values fail
Status: Not a bug
Type: Bug
Package: Math related
Operating System: Linux
PHP Version: 4.3.9
Block user comment: N
Private report: N
New Comment:
How is this not a bug ?
At the very least it deserves an explanation.
Previous Comments:
------------------------------------------------------------------------
[2005-02-23 16:25:34] weber at mapsolute dot com
Ups, my misstake, I saw it after submitting (hopefully nobody reads it ;-)).
------------------------------------------------------------------------
[2005-02-23 16:22:17] weber at mapsolute dot com
Description:
------------
If you compare two equal floating point values for uneven the compare fails (it returns true) and I
can see no reason why. I've checked the PHP manual
(http://uk.php.net/manual/en/language.types.float.php), but this doesn't explain the behave.
The two numbers below must be binary equal, independend if you save them as single or double
precision floating point numbers and even if you would store them as double and convert into float
for comparing, they must stay binary equal. Even as string they must be binary equal.
Therefore comparing them should return true in any case.
Quote from http://en.wikipedia.org/wiki/IEEE_754:
Comparing floating-point numbers
An interesting feature of this particular representation is that it makes comparisons of numbers of
the same sign which are not NaNs simple. For positive numbers (the sign bit is 0) a and b, then a
< b whenever the unsigned binary integers with the same bit patterns as a and b are also ordered
the same way. In other words if you are comparing two positive floating-point numbers (known not to
be NaNs) you can just use an unsigned binary integer comparison using the same bits.
Reproduce code:
---------------
<html><body><?php
$a = 437.674240047;
$b = 437.674240047;
if( $a != $b ) echo "<p>OK</p>";
else echo "<p>WRONG</p>";
var_dump($a); echo "<br>"; var_dump($b);
?></body></html>
Expected result:
----------------
OK
float(437.674240047)
float(437.674240047)
Actual result:
--------------
WRONG
float(437.674240047)
float(437.674240047)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=32082&edit=1