php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68111 json_decode introduces floating point precision error
Submitted: 2014-09-27 05:00 UTC Modified: 2014-09-29 17:25 UTC
From: ryan at ryanparman dot com Assigned:
Status: Not a bug Package: JSON related
PHP Version: 5.5.17 OS: OS X
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ryan at ryanparman dot com
New email:
PHP Version: OS:

 

 [2014-09-27 05:00 UTC] ryan at ryanparman dot com
Description:
------------
When decoding a JSON string with json_decode(), a floating point precision error gets introduced.

Issue identified in PHP 5.5.17, and verified that it does NOT occur in 5.4.33.

Test script:
---------------
$data = json_decode('{"value": 4565.85}');
echo $data->value;

Expected result:
----------------
I expected `$data->value` to have a value of `4565.85`.

Actual result:
--------------
The actual value of `$data->value` is `4565.8500000000004`.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-09-27 05:05 UTC] ryan at ryanparman dot com
-Package: Scripting Engine problem +Package: JSON related
 [2014-09-27 05:05 UTC] ryan at ryanparman dot com
This should be filed under JSON-related.
 [2014-09-29 16:09 UTC] aharvey@php.net
-Status: Open +Status: Not a bug
 [2014-09-29 16:09 UTC] aharvey@php.net
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/

Thank you for your interest in PHP.

It's going to be the same value on both, just with different precision settings controlling the output.
 [2014-09-29 17:25 UTC] ryan at ryanparman dot com
So, I would like to better understand why two versions of PHP give me different results.

----

$ php --version
PHP 5.5.17 (cli) (built: Sep 19 2014 00:50:57) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
$ php -i | grep -i precision
precision => 14 => 14
serialize_precision => 17 => 17

----

$ php --version
PHP 5.4.33 (cli) (built: Sep 20 2014 16:20:03) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies
    with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
$ php -i | grep -i precision
precision => 14 => 14
serialize_precision => 100 => 100

----

Could it be because the `serialize_precision` value is different?
 [2014-11-19 22:22 UTC] admin at bittylicious dot com
I've created a partially-related feature request for this to have an option for json_decode to handle all numbers as strings. If you're never going to use the result as a double internally, this can be useful.

https://bugs.php.net/bug.php?id=68456
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 27 01:01:28 2024 UTC