3 ms·
In PHP's case, it converts the value to a printable (string) representation, and in so doing rounds off to a precision of 14 digits. http://lxr.php.net/xref/PH
by Joeri 11y ago
In PHP's case, it converts the value to a printable (string) representation, and in so doing rounds off to a precision of 14 digits.
http://lxr.php.net/xref/PHP_5_4/Zend/zend_operators.c#2142 http://lxr.php.net/xref/PHP_5_4/Zend/zend_operators.c#2142
The "mathematically" correct answer is 0.30000000000000004 according to the IEEE-754 spec.
- lsaferite 11y agoIf you want PHP to print it like the other languages you have to set the output precision high enough. The default, as you mentioned, is 14 but if you set it to 17 then you get the 'correct' output. https://3v4l.org/TRcJB https://3v4l.org/TRcJB http://php.net/manual/en/ini.core.php#ini.precision http://php.net/manual/en/ini.core.php#ini.precision
- McGlockenshire 11y agoAnother option is to use printf, as like other languages it can take a precision argument. printf('%.17f', 0.1 + 0.2); // 0.30000000000000004