php.net |  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 Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: joos at frisbee dot nl
New email:
PHP Version: OS:

 

 [2018-08-29 14:36 UTC] joos at frisbee dot nl
Description:
------------
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';
}
else
{
   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

Patches

Pull Requests

History

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

Thank you for your interest in PHP.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 12 14:01:28 2024 UTC