php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #17282 Identical float values are not identical when compared
Submitted: 2002-05-16 22:23 UTC Modified: 2002-05-17 09:20 UTC
From: david at acz dot org Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.2.1 OS: FreeBSD 4.4-STABLE
Private report: No CVE-ID: None
 [2002-05-16 22:23 UTC] david at acz dot org
This problem also occurs on PHP 4.1.2.

The following code fragment produces very odd results:

<?
    $a = array(41.96, 20.97, 20.99);
    $t = 0;
    for($i = 1; $i < count($a); $i++)
        $t += abs($a[$i]);
    echo "$t != $a[0]\n";
    var_dump($t != $a[0]);
    echo "t = ";
    var_dump($t);
    echo "a[0] = ";
    var_dump($a[0]);
?>

The results are shown below:

41.96 != 41.96
bool(true)
t = float(41.96)
a[0] = float(41.96)

Both variables appear to be the same type, and look the same, yet compare incorrectly with both typed and untyped comparisons.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-05-17 02:16 UTC] 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
 [2002-05-17 02:36 UTC] david at acz dot 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:41 UTC] 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 03:22 UTC] corey at lik-sang dot 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 03:25 UTC] corey at acz dot 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 04:11 UTC] corey at lik-sang dot com
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.
 [2002-05-17 09:20 UTC] rasmus@php.net
So change your precision setting in your php.ini file if you want the higher display precision.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Sun Oct 25 11:01:23 2020 UTC