Bug #17282 Updated: Identical float values are not identical when compared
| From: | corey at lik-sang dot com | Date: | Fri, 17 May 2002 08:11:51 +0000 |
| Subject: | Bug #17282 Updated: Identical float values are not identical when compared | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-7906@lists.php.net to get a copy of this message | ||
ID: 17282
Updated by: corey@lik-sang.com
Reported By: david@acz.org
Status: Bogus
Bug Type: Scripting Engine problem
Operating System: FreeBSD 4.4-STABLE
PHP Version: 4.2.1
New Comment:
You're right, floats aren't precise, but neither is your output. I
don't know if it is rounded or something odd, but if you say it
operates the same as in C, I challenge you to test adding 20.99 and
20.97 in C and compare it to 41.96.
You will find that they are not the same BUT c does the correct
response by outputting it's float value of 41.959999 instead of 41.96
In your case, the comparison should be on the value that is used for
display and etc.
Previous Comments:
------------------------------------------------------------------------
[2002-05-17 03:25:19] corey@acz.org
sorry, didn't see your earlier update.
However, I'd just like to comment that it is rediculous that a feature
exists that is known to be false.
------------------------------------------------------------------------
[2002-05-17 03:22:55] corey@lik-sang.com
Ah, but the source is very interesting.
<?
$b = 41.00;
$b2 = 00.96;
$c = array(41.96,41.00,00.96,0.00,41.96);
for($i=1;$i<3;$i++)
$c[3]+=$c[$i];
echo "<p>";
var_dump($c[3] != $c[0]);
echo "<p>";
var_dump($c[4] != $c[0]);
echo "<p>";
$b2 = $b + $b2;
var_dump($c[0] != $b2);
echo "<p>";
var_dump($c[3] != $b2);
?>
OUTPUT:
bool(false)
bool(false)
bool(false)
bool(false)
Why is it those 2 numbers only?
------------------------------------------------------------------------
[2002-05-17 02:41:23] derick@php.net
Actually this is documented:
http://www.php.net/manual/en/language.types.float.php
> I am well aware that there can be errors due to loss of precision,
but
> in that case, the number should not appear to be _exactly the same_.
> There should be some way to tell that they are different.
You can tell they are different by comparing them, which obviously
almost always fails, but i fyou do this:
if (41.96 == 41.96)
it will return true ofcourse.
There was some discussion on this topic before, and the consensus of
the list was not to change anything related to this.
regards,
Derick
------------------------------------------------------------------------
[2002-05-17 02:36:19] david@acz.org
That does not seem right. The numbers appear to be exactly the same.
Even if they can't be compared as floats, comparing untyped should
work, because they look exactly the same (when var_dump'd and
printed).
Even if this is in fact bogus, it needs to be documented.
I am well aware that there can be errors due to loss of precision, but
in that case, the number should not appear to be _exactly the same_.
There should be some way to tell that they are different.
------------------------------------------------------------------------
[2002-05-17 02:16:22] derick@php.net
You can't compare floats like this, a float is an approximation of the
number, but never exact the number.
This is not a bug, this wouldn't work in most languages, including C ->
Bogus
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
http://bugs.php.net/17282
--
Edit this bug report at http://bugs.php.net/?id=17282&edit=1