Doc #72567 [Csd]: PHP 7.1 changes in json_encode() wrt. encoding floats undocumented

From: Date: Sun, 10 Jul 2016 09:30:28 +0000
Subject: Doc #72567 [Csd]: PHP 7.1 changes in json_encode() wrt. encoding floats undocumented
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-13700@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72567&edit=1 ID: 72567 User updated by: sjon at hortensius dot net Reported by: sjon at hortensius dot net Summary: PHP 7.1 changes in json_encode() wrt. encoding floats undocumented Status: Closed Type: Documentation Problem Package: JSON related Operating System: 3v4l.org PHP Version: PHP 7.1 Assigned To: cmb Block user comment: N Private report: N New Comment: Thanks for the hints. I previously configured serialize_precision=24 (on 3v4l.org) to iron out differences between versions but I'll remove that; making it behave better in 7.1. It seems to be pretty stable for non-eol versions anyway Previous Comments: ------------------------------------------------------------------------ [2016-07-09 15:09:12] nikic@php.net Instead of changing serialize_precision to 14, it is recommended to leave it at the new default value of -1. ------------------------------------------------------------------------ [2016-07-09 12:21:06] cmb@php.net This bug has been fixed in the documentation's XML sources. Since the online and downloadable versions of the documentation need some time to get updated, we would like to ask you to be a bit patient. Thank you for the report, and for helping us make our documentation better. ------------------------------------------------------------------------ [2016-07-09 12:20:40] cmb@php.net Automatic comment from SVN on behalf of cmb Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=339594 Log: Fix #72567: PHP 7.1 changes in json_encode() wrt. encoding floats undocumented ------------------------------------------------------------------------ [2016-07-09 11:58:09] cmb@php.net json_encode() now uses the ini option serialize_precision instead of precision when encoding double values. That behavioral change was a deliberate decision proposed and voted upon in RFC "More precise float value handling"[1]. Use ini_set('serialize_precision', 14) to restore the old behavior, see <https://3v4l.org/3ZuTQ>. [1] <https://wiki.php.net/rfc/precise_float_value> ------------------------------------------------------------------------ [2016-07-09 10:34:34] sjon at hortensius dot net Description: ------------ Not sure what the correct terminology is for this; but it seems using json_encode with floats will result in 'rounding errors' as illustrated by the script below. This happens since alpha3 as can be seen on https://3v4l.org/q3SU6 Using JSON_NUMERIC_CHECK has no influence as can be seen on the original https://3v4l.org/TvGbe Test script: --------------- <?php echo json_encode(array(0.1, 0.2, 0.3, 0.4, 0.5, 0.6,)); Expected result: ---------------- [0.1,0.2,0.3,0.4,0.5,0.6] Actual result: -------------- [0.100000000000000005551115,0.20000000000000001110223,0.29999999999999998889777,0.40000000000000002220446,0.5,0.59999999999999997779554] ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=72567&edit=1

« previous php.doc.bugs (#13700) next »