3

When I set PHP's precision setting to values 18 or higher (whether in php.ini or at runtime), the round() function yields unexpected results. Is this a bug; or what am I missing?

Results for e.g. rounding the float 12.4886724321 to 4 decimal precision are as follows:

14: 12.4887
...
17: 12.4887
18: 12.4886999999999997
19: 12.48869999999999969
20: 12.48869999999999969
21: 12.4886999999999996902
22: 12.4886999999999996902
23: 12.488699999999999690203
24: 12.4886999999999996902034

Test case as follows:

$floaty = 12.4886724321;

for ($i = 14; $i <= 24; $i++) {
    ini_set('precision', $i);
    echo ini_get('precision') . ': ';
    echo round($floaty, 4)  . "\n";
}

Even if I set my $floaty to just 12.4, I'll get 18: 12.4800000000000004 etc. for "extrapolated" decimals. What exactly is this twilight zone we're entering at precisions 18 or greater? I do know how to clean things up for output, but I'd like to know why this happens, and whether it's intended behavior.

Tested on PHP 7.0.2 @ W7x64 and PHP 5.6.8 @ CentOS 6.5 with identical results. number_format() doesn't do this, suppose it's more blunt (or non-mathematical) in how it truncates the decimals.

EDIT: Seems to happen with all math ops? 1234.56 - 1233.03 iterated at different precisions:

12: 1.53
13: 1.53
14: 1.53
15: 1.52999999999997
16: 1.529999999999973
17: 1.5299999999999727
18: 1.52999999999997272
Markus AO
  • 4,771
  • 2
  • 18
  • 29
  • 5
    Possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Sami Kuhmonen Jan 31 '16 at 07:12
  • Very informative, thank you. Testing to double-check, I notice I don't get the anomalous extra digits when I iterate the test on e.g. `77.125`, (which is `1234/2^4`). Basically this is the way it is then; and now I know why. And *note to self*: `round()` is a `math` function, not an `output formatting` function... Switching to `number_format()` then. – Markus AO Jan 31 '16 at 07:48

1 Answers1

1

Floats in base 10, like 0.1 or 0.7, do not have an exact representation as float numbers in base 2

see http://floating-point-gui.de

PHP Docs to float

see Floating point precision 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 propagation 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 do not compare floating point numbers directly for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available.

lumos0815
  • 3,908
  • 2
  • 25
  • 25