|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74925 Mismatching floating values
Submitted: 2017-07-14 14:42 UTC Modified: 2017-07-17 16:47 UTC
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: tuepp at gmx dot de Assigned:
Status: Not a bug Package: PHP Language Specification
PHP Version: 7.0.21 OS: Windows
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: tuepp at gmx dot de
New email:
PHP Version: OS:


 [2017-07-14 14:42 UTC] tuepp at gmx dot de
Floating number comparison fails for numbers that have to be rounded internal.
This bug may be over 10 years old. I've reported it 5 years ago and it is still not fixed. Now I have an example with one instead of 3 lines of code what might be more shoking. I've documented this bug in my final exam 6 years ago and it feeled like I got some extra points for it - so thank you. But now it's really time to fix it! I'm not sure if I'll report it a third time in another 5 years... So go, get it!

Test script:
echo (54.83 + 0.01 == 54.84) ? 'ok' : 'fail'; //fail
echo "<br>\n";
echo ((3 * 202.3) == 606.9) ? 'ok' : 'fail'; //fail
echo "<br>\n";
for($v = -1; $v < 0; $v += 0.2){
	echo "$v; "; // -1; -0.8; -0.6; -0.4; -0.2; -5.5511151231258E-17;

Expected result:
-1; -0.8; -0.6; -0.4; -0.2 

Actual result:
-1; -0.8; -0.6; -0.4; -0.2; -5.5511151231258E-17;


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-07-14 14:49 UTC]
-Status: Open +Status: Not a bug
 [2017-07-14 14:49 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.

 [2017-07-17 16:41 UTC] tuepp at gmx dot de
I know about this issue - and I wonder why there shouldn't be some internal rounding-mechanism.

"Thank you for your interest in PHP." - that must be a bad joke...
 [2017-07-17 16:47 UTC]
Round to what? For display purposes you can set a precision, but arbitrarily doing internal rounding would lead to chaos. I don't know of any programming language that does that. Pick any language and try your example and you will get similar results. This is how computers handle floating point natively. It is up to the programmer to apply whatever rounding is appropriate for their situation.
 [2017-07-18 18:28 UTC] neclimdul at gmail dot com
I thought was weird for a sec looking at the output of php's values but was then reminded that's just the display misrepesenting the internal value. As Rasmus stated the values are consistent with other languages.

What seems to be different in Ruby and Node is that they auto expand out the value and show the float weirdness when you just print the value. C doesn't and PHP is more in line with its behavior.

Basically, try this for your test script.
var_dump(54.83 + 0.01);
printf("%.18e\n", 54.83 + 0.01);
var_dump(54.83 + 0.01 == 54.84);
var_dump(3 * 202.3);
printf("%.18e\n", (3 * 202.3));
var_dump((3 * 202.3) == 606.9);
for($v = -1; $v < 0; $v += 0.2){
  printf("%.18e\n", $v);
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 22 00:01:36 2024 UTC