round() breaks when PHP precision is set to 18+

守給你的承諾、 提交于 2019-12-13 14:28:41

问题


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

回答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.



来源:https://stackoverflow.com/questions/35111302/round-breaks-when-php-precision-is-set-to-18

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!