Bug #72567 [NEW]: json_encode results in rounding errors when encoding floats

From: Date: Sat, 09 Jul 2016 10:34:38 +0000
Subject: Bug #72567 [NEW]: json_encode results in rounding errors when encoding floats
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-202142@lists.php.net to get a copy of this message
From:             sjon at hortensius dot net
Operating system: 3v4l.org
PHP version:      Next Major Version
Package:          JSON related
Bug Type:         Bug
Bug description:json_encode results in rounding errors when encoding floats

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 bug report at https://bugs.php.net/bug.php?id=72567&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=72567&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=72567&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=72567&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=72567&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=72567&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=72567&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=72567&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=72567&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=72567&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=72567&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=72567&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=72567&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=72567&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=72567&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=72567&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=72567&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=72567&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=72567&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=72567&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=72567&r=mysqlcfg



Thread (1 message)

  • sjon at hortensius dot net
« previous php.bugs (#202142) next »