PHP floating point precision: Is var_dump secretly rounding and how can I debug precisley then?

老子叫甜甜 提交于 2020-04-13 03:58:47

问题


That floating point numbers in PHP are inaccurate is well known (http://php.net/manual/de/language.types.float.php), however I am a bit unsatisfied after the following experiment:

var_dump((2.30 * 100)); // float(230)
var_dump(round(2.30 * 100)); // float(230)
var_dump(ceil(2.30 * 100)); // float(230)
var_dump(intval(2.30 * 100)); // int(229)
var_dump((int)(2.30 * 100)); // int(229)
var_dump(floor(2.30 * 100)); // float(229)

The internal representation must be something like 229.999998.

var_dump((2.30 * -100)); // float(-230)
var_dump(round(2.30 * -100)); // float(-230)
var_dump(ceil(2.30 * -100)); // float(-229)
var_dump(intval(2.30 * -100)); // int(-229)
var_dump((int)(2.30 * -100)); // int(-229)
var_dump(floor(2.30 * -100)); // float(-230)

The internal representation must be something like -229.999998.

Ok as far as I understand - integer casting as well as the intval function simply cut of everything behind the point. Good to know.

However var_dump() gives me a value of 230 though the real value must be different according to those results.

Now have a look at this:

$a = 230.0;

var_dump($a); // float(230)
var_dump((int) $a); // int(230)

That means the internal representation of the floating point number must be different here. If I want to know the exact value of a float therefore I can not debug using var_dump as I am used to right? How can I debug an exact float value?


回答1:


You can try to use number_format it won't be perfect since you have to provide number of decimals, but should help.

echo number_format(8-6.4, 50);

1.59999999999999964472863211994990706443786621093750

echo number_format(2.3*100, 50);

229.99999999999997157829056959599256515502929687500000

Edit: As the number of decimal places is varying (this also depends on the system used) the following might be useful - gets the full number for sure and removes trailing zeros:

echo rtrim(number_format(1.0/3.432, 100),0);

0.29137529137529138978379705804400146007537841796875



来源:https://stackoverflow.com/questions/49981651/php-floating-point-precision-is-var-dump-secretly-rounding-and-how-can-i-debug

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