0

I would expect all below comparisons to be bool (true) but they are not. Can anyone explain this?

test.php

<?php

$f = 12;
$f += 5.95;
$f += 5.95;
$f += 5.95;

echo 'var_dump($f) = ';
var_dump($f);

echo 'var_dump($f == \'29.85\') = ';
var_dump($f == '29.85');

echo 'var_dump($f == 29.85) = ';
var_dump($f == 29.85);

echo 'var_dump($f == (float)\'29.85\') = ';
var_dump($f == (float)'29.85');

echo 'var_dump($f == \'29.85\') = ';
var_dump((string)$f == '29.85');

echo 'var_dump(round($f, 2) == \'29.85\') = ';
var_dump(round($f, 2) == '29.85');

$ php test.php

var_dump($f) = float(29.85)
var_dump($f == '29.85') = bool(false)
var_dump($f == 29.85) = bool(false)
var_dump($f == (float)'29.85') = bool(false)
var_dump($f == '29.85') = bool(true)
var_dump(round($f, 2) == '29.85') = bool(true)

$ php -v

PHP 5.2.14 (cli) (built: Jul 23 2010 15:23:00)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
    with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
hakre
  • 193,403
  • 52
  • 435
  • 836
kralos
  • 23
  • 3

3 Answers3

5

Floating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and, of course, error progragation must be considered when several operations are compounded.

Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....

So never trust floating number results to the last digit, and never compare floating point numbers for equality.

PHP documentation page

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
1

When you compare the values as strings, both sides are '29.85', so you get true. So far so easy.

Comparing by numerical value leads you into the land of binary floating point value representations. Since numbers are stored base-2, any real number that is not expressible in a finite binary expansion cannot be precisely represented by a floating point number.

In other words, every number that cannot be written as a fraction of integers where the denominator is a power of 2 cannot be thus represented. This includes 1/5 and 1/10 and 597/20 (which is 29.85).

Because these numbers cannot be precisely represented, the outcome of operations involving such numbers depends on the order of the operations and on rounding and truncation errors, and so for example .1 + .1 + .1 is not the same as .3, and similar for your computation.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

As @Shakti Singh says, floating point numbers are not stored precisely, so I'd guess $f is actually stored as something like 29.849999999999999..., so is not exactly equal to 29.85.

Also, this code line looks wrong:

echo 'var_dump($f == \'29.85\') = ';
var_dump($f == 29.85);

I assume it should be:

echo 'var_dump($f == \'29.85\') = ';
var_dump($f == '29.85');
Nick Shaw
  • 2,083
  • 19
  • 27
  • oversight, i've updated and yes the result is still bool (false) for var_dump($f == '29.85'); – kralos Jul 27 '11 at 12:49