Bug #76810 [Opn->Nab]: Binary difference between numbers
Edit report at https://bugs.php.net/bug.php?id=76810&edit=1
ID: 76810
Updated by: cmb@php.net
Reported by: joos at frisbee dot nl
Summary: Binary difference between numbers
-Status: Open
+Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: Windows / Linux
PHP Version: 5.6.37
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2018-08-29 14:36:03] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76810&edit=1
Thread (2 messages)