Bug #74925 [Com]: Mismatching floating values

From: Date: Tue, 18 Jul 2017 18:28:18 +0000
Subject: Bug #74925 [Com]: Mismatching floating values
References: 1  Groups: php.standards 
Request: Send a blank email to standards-+get-1280@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74925&edit=1 ID: 74925 Comment by: neclimdul at gmail dot com Reported by: tuepp at gmx dot de Summary: Mismatching floating values Status: Not a bug Type: Bug Package: PHP Language Specification Operating System: Windows PHP Version: 7.0.21 Block user comment: N Private report: N New Comment: 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. https://gist.github.com/neclimdul/79d887d655ee5cec4bec50faa94c4382 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. --------------- <?php 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); var_dump($v); } Results --------------- float(54.84) 5.483999999999999631e+1 bool(false) float(606.9) 6.069000000000000909e+2 bool(false) -1.000000000000000000e+0 int(-1) -8.000000000000000444e-1 float(-0.8) -6.000000000000000888e-1 float(-0.6) -4.000000000000000777e-1 float(-0.4) -2.000000000000000666e-1 float(-0.2) -5.551115123125782702e-17 float(-5.5511151231258E-17) Previous Comments: ------------------------------------------------------------------------ [2017-07-17 16:47:54] rasmus@php.net 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-17 16:41:19] 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-14 14:49:38] krakjoe@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. ------------------------------------------------------------------------ [2017-07-14 14:42:09] tuepp at gmx dot de Description: ------------ 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: --------------- <?php 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: ---------------- ok ok -1; -0.8; -0.6; -0.4; -0.2 Actual result: -------------- fail fail -1; -0.8; -0.6; -0.4; -0.2; -5.5511151231258E-17; ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=74925&edit=1

« previous php.standards (#1280) next »