Bug #75800 [Nab]: json_encode produces unexpected results with summed floats

From: Date: Fri, 12 Jan 2018 11:08:16 +0000
Subject: Bug #75800 [Nab]: json_encode produces unexpected results with summed floats
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-213490@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75800&edit=1 ID: 75800 Updated by: requinix@php.net Reported by: yoch at yoch dot org Summary: json_encode produces unexpected results with summed floats Status: Not a bug Type: Bug Package: JSON related Operating System: Ubuntu 16.04.3 LTS PHP Version: 7.2.1 Block user comment: N Private report: N New Comment: > In this case i believe "serialize_precision" should default to the > "precision" value to produce predictable results > out of the box. They serve two different purposes. serialize_precision is meant to be used in situations where the complete value is needed, such as serialization (obviously). precision is more for general-purpose conversions and output where some rounding is desirable. What changed in PHP 7.1 was that json_encode() with floats should try to be as precise as possible because it acts more like serialization than as a float-to-string conversion. https://wiki.php.net/rfc/precise_float_value Previous Comments: ------------------------------------------------------------------------ [2018-01-12 10:41:06] yoch at yoch dot org Damian, thanks for the explanation. In this case i believe "serialize_precision" should default to the "precision" value to produce predictable results out of the box. ------------------------------------------------------------------------ [2018-01-11 15:21:50] spam2 at rhsoft dot net and why "precision" defaults to 14 while serialize_precision defaults to -1 ------------------------------------------------------------------------ [2018-01-11 15:05:23] requinix@php.net We have a template response for that one too, actually: Floating point values have a limited precision. Hence a value might not have the same string representation after any processing. That also includes writing a floating point value in your script and directly printing it without any mathematical operations. If you would like to know more about "floats" and what IEEE 754 is, read this: http://www.floating-point-gui.de/ -1 is "unlimited" precision, meaning that you can see the full float value - including possible error introduced during math. The two json_encode() tests produce different results because of the inconsis-- I mean, the magic of floating-point math. To get the expected result in all cases you need a precision and serialize_precision of some value between 3 and 16, such as 14. 2.4899999999999998 ^ ^ 3 16 ------------------------------------------------------------------------ [2018-01-11 12:45:30] yoch at yoch dot org Does that means that setting both "precision" and "serialize_precision" to "-1" must produce the expected results? It does not right now. ------------------------------------------------------------------------ [2018-01-11 12:27:57] requinix@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php http://php.net/manual/en/function.json-encode.php > Changelog > 7.1.0 serialize_precision is used instead of precision when encoding double values. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=75800 -- Edit this bug report at https://bugs.php.net/bug.php?id=75800&edit=1

« previous php.bugs (#213490) next »