php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73680 json_encode on window error
Submitted: 2016-12-08 01:56 UTC Modified: 2016-12-08 05:30 UTC
From: xltxlm at qq dot com Assigned:
Status: Not a bug Package: JSON related
PHP Version: 7.1.0 OS: Irrelevant
Private report: No CVE-ID: None
 [2016-12-08 01:56 UTC] xltxlm at qq dot com
Description:
------------
var_dump(json_encode([100.1]));

Expected result:
----------------
string(7) "[100.1]"

Actual result:
--------------
string(20) "[100.09999999999999]"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-12-08 03:10 UTC] yohgaki@php.net
-Status: Open +Status: Not a bug -Operating System: window +Operating System: Irrelevant
 [2016-12-08 03:10 UTC] yohgaki@php.net
Use lower serialize_presision if you do not want this kind of outputs.

i.e. IEEE float value is not "precise" float value.
 [2016-12-08 05:30 UTC] yohgaki@php.net
The reason behind this change.

JSON is cross platform serialization and most systems uses IEEE 754 for float. Older PHP JSON truncated float value by "precision" ini setting which is less precise with regard to IEEE 754. i.e. Older PHP was losing precision by default.

PHP 7.1 uses "serialize_precision" which is the same precision for most systems. Therefore, PHP 7.1 or later JSON can handle float value more precisely than before.

If users want to use the value displayed by "echo $float_val;", set the same "prevision" ini value to "serialize_precision". Please note that this affects serialize() also.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue Dec 10 07:01:24 2019 UTC