Bug #74925 [Nab]: Mismatching floating values

[email protected] Mon, 17 Jul 2017 16:47:58 GMT
Newsgroups php.standards
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=74925&edit=1

 ID:                 74925
 Updated by:         [email protected]
 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:

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.


Previous Comments:
------------------------------------------------------------------------
[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] [email protected]

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