|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76810 Binary difference between numbers
Submitted: 2018-08-29 14:36 UTC Modified: 2018-08-29 14:37 UTC
From: joos at frisbee dot nl Assigned: cmb (profile)
Status: Not a bug Package: *General Issues
PHP Version: 5.6.37 OS: Windows / Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: joos at frisbee dot nl
New email:
PHP Version: OS:


 [2018-08-29 14:36 UTC] joos at frisbee dot nl
If you execute the test script there is a difference in the binary representation:

The first line will output: cdcccccccccc2640
The second line will output: cccccccccccc2640

The string representation both show 11.4.
The comparison will say that the result of the first line is higher than the second line.
But that is not true, they should be equal.

This only seems to happen when you use 0.95 or 0.951.
If you use 0.9511 or 0.94 the result is as expected (if you recalculate the expected result).
If you use 0.951 the result is the same as with 0.95.

This has been tested on both 5.6.37 and 7.1.21.

Test script:
var_dump(bin2hex(pack('d', 11.4)));
var_dump(bin2hex(pack('d', 0.95*12)));

if(11.4 > 0.95*12)
   echo 'It\'s bigger';
   echo 'It\'s the same or lower';//Should echo this, but it will echo It's bigger

Expected result:
It's the same or lower should be echo-ed.

Actual result:
It's bigger will be echo-ed


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2018-08-29 14:37 UTC]
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2018-08-29 14:37 UTC]
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:

Thank you for your interest in PHP.
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 10:01:25 2019 UTC