PHP
<?php echo (-9.341+2.111);
Result -7.23
Javascript
console.log(2.111-9.341)
Result -7.229999999999999
I'm aware of floating Arithmetic, but why vanilla PHP give exact answer and Javascript (tested on Google Chrome console) not?
PHP
<?php echo (-9.341+2.111);
Result -7.23
Javascript
console.log(2.111-9.341)
Result -7.229999999999999
I'm aware of floating Arithmetic, but why vanilla PHP give exact answer and Javascript (tested on Google Chrome console) not?
It is php's echo
that is rounding:
var_dump(sprintf('%.20f', -9.341+2.111)); // string(23) "-7.22999999999999864997"
In php sources it looks that echo
would convert a floating point number to a string using the following conversion:
str = zend_strpprintf(0, "%.*G", (int) EG(precision), dval);
Which basically means either scientific notation or %f
(see precision
configuration directive, 14 by default).