Bug #66132 [Com]: X*5/X where X is floating point number is greater than 5
[email protected] ("utilmind at gmail dot com") Thu, 21 Nov 2013 15:07:46 GMT
| Newsgroups | php.gtk.dev |
|---|---|
| Message-ID | <[email protected]> |
Edit report at https://bugs.php.net/bug.php?id=66132&edit=1
ID: 66132
Comment by: utilmind at gmail dot com
Reported by: utilmind at gmail dot com
Summary: X*5/X where X is floating point number is greater
than 5
Status: Not a bug
Type: Bug
Package: PHP-GTK related
Operating System: Any, tested in Win32 and CentOS
PHP Version: 5.4.22
Block user comment: N
Private report: N
New Comment:
The "ini_set("precision", 17);" explains the origin of the problem, but does not solves it.
The problem is not that floating point operations makes little inaccuracies.
My point is that, if PHP with low precision setting rounds values for visual representation of inaccurate numbers, it should also round them on compare operations like < > and ==. Otherwise, we currently getting a value, which looks exactly like "5", but actually it little bit more than "5".
Currently following type conversion operation fixes the problem:
$n = (float)(string)(3.89880952381*5/3.89880952381);
but it's not ideal. The "precision" option of php.ini should also affect on logical comparison operations.
Previous Comments:
------------------------------------------------------------------------
[2013-11-21 14:41:11] [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.
php > ini_set("precision", 17);
php > $n = (3.89880952381*5/3.89880952381); // result will be a very bit greater than 5
php > var_dump($n);
float(5.0000000000000009)
------------------------------------------------------------------------
[2013-11-21 00:19:36] [email protected]
This is an PHP main package bug
Confirmed
Version: PHP 5.5.1-2 (built: Aug 5 2013 14:10:28)
OS: Linux version 3.10-2-amd64 #1 SMP Debian 3.10.5-1 (2013-08-07)
------------------------------------------------------------------------
[2013-11-20 23:53:46] utilmind at gmail dot com
Description:
------------
It's probably bug with conversion from floating point number, or bug with comparision between types. We know that X*5/5 is 5. Even PHP returns 5, however, sometimes, returned value appears to be greater than 5.
Test script:
---------------
$n = (3.89880952381*5/3.89880952381); // result will be exactly 5
print $n.'!=5 ? '.($n>5); // here it appears that 5 is greater than 5
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66132&edit=1