Bug #68111 [Nab]: json_decode introduces floating point precision error

From: Date: Mon, 29 Sep 2014 17:25:20 +0000
Subject: Bug #68111 [Nab]: json_decode introduces floating point precision error
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-187757@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68111&edit=1 ID: 68111 User updated by: ryan at ryanparman dot com Reported by: ryan at ryanparman dot com Summary: json_decode introduces floating point precision error Status: Not a bug Type: Bug Package: JSON related Operating System: OS X PHP Version: 5.5.17 Block user comment: N Private report: N New Comment: 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? Previous Comments: ------------------------------------------------------------------------ [2014-09-29 16:09:18] 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-27 05:05:07] ryan at ryanparman dot com This should be filed under JSON-related. ------------------------------------------------------------------------ [2014-09-27 05:00:56] 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. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=68111&edit=1

« previous php.bugs (#187757) next »