php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76239 Only 32 ~ 122 / whole number will appear, small digital specific.13 and 0.02
Submitted: 2018-04-19 03:42 UTC Modified: 2018-04-19 11:40 UTC
From: 1203860880 at qq dot com Assigned: cmb (profile)
Status: Not a bug Package: json (PECL)
PHP Version: 7.2.4 OS: win10 (64bit)
Private report: No CVE-ID: None
 [2018-04-19 03:42 UTC] 1203860880 at qq dot com
Description:
------------
<?php
/*
A programmer who does not understand English
The description is with Machine Translation
*/
// Only 32 ~ 122 / whole number will appear, small digital specific.13 and 0.02
                $c = 122.13 + 0.02;
                print_r($c);  //  105.15
                echo "<br />";
                echo json_encode($c);  //  105.14999999999999
                echo "<br />";
                echo json_encode(105.15);  //  105.15

Test script:
---------------
<?php
/*
A programmer who does not understand English
The description is with Machine Translation

PHP Version 7.2.0
json Version  1.6.0
*/
// Only 32 ~ 122 / whole number will appear, small digital specific.13 and 0.02
                $c = 122.13 + 0.02;
                print_r($c);  //  105.15
                echo "<br />";
                echo json_encode($c);  //  105.14999999999999
                echo "<br />";
                echo json_encode(105.15);  //  105.15

Expected result:
----------------
                echo json_encode($c);  //  105.15

Actual result:
--------------
echo json_encode($c);  //  105.14999999999999

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-04-19 11:40 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2018-04-19 11:40 UTC] cmb@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/

In this case you alleviate the issues by setting
serialize_precision and precision to the same values, see
<https://3v4l.org/bGaCZ>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC