#32082 [NEW]: Comparing of floating point values fail
| From: | weber at mapsolute dot com | Date: | Wed, 23 Feb 2005 15:22:19 +0000 |
| Subject: | #32082 [NEW]: Comparing of floating point values fail | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-74469@lists.php.net to get a copy of this message | ||
From: weber at mapsolute dot com
Operating system: Linux
PHP version: 4.3.9
PHP Bug Type: Math related
Bug description: Comparing of floating point values fail
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 bug report at http://bugs.php.net/?id=32082&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=32082&r=trysnapshot4
Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=32082&r=trysnapshot50
Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=32082&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=32082&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=32082&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=32082&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=32082&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=32082&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=32082&r=support
Expected behavior: http://bugs.php.net/fix.php?id=32082&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=32082&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=32082&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=32082&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=32082&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=32082&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=32082&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=32082&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=32082&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=32082&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=32082&r=mysqlcfg