[PHP-BUG] Bug #66132 [NEW]: X*5/X where X is floating point number is greater than 5

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[PHP-BUG] Bug #66132 [NEW]: X*5/X where X is floating point number is greater than 5

Guthrie at att dot net
From:             utilmind at gmail dot com
Operating system: Any, tested in Win32 and CentOS
PHP version:      5.4.22
Package:          PHP-GTK related
Bug Type:         Bug
Bug description:X*5/X where X is floating point number is greater than 5

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 bug report at https://bugs.php.net/bug.php?id=66132&edit=1
--
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=66132&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=66132&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=66132&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=66132&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=66132&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=66132&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=66132&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=66132&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=66132&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=66132&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=66132&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=66132&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=66132&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=66132&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=66132&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=66132&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=66132&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=66132&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=66132&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=66132&r=mysqlcfg


--
PHP-GTK Development Mailing List (http://gtk.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply | Threaded
Open this post in threaded view
|

Bug #66132 [Com]: X*5/X where X is floating point number is greater than 5

Guthrie at att dot net
Edit report at https://bugs.php.net/bug.php?id=66132&edit=1

 ID:                 66132
 Comment by:         [hidden email]
 Reported by:        utilmind at gmail dot com
 Summary:            X*5/X where X is floating point number is greater
                     than 5
 Status:             Open
 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:

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)


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

--
PHP-GTK Development Mailing List (http://gtk.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply | Threaded
Open this post in threaded view
|

Bug #66132 [Opn->Nab]: X*5/X where X is floating point number is greater than 5

bwoebi
In reply to this post by Guthrie at att dot net
Edit report at https://bugs.php.net/bug.php?id=66132&edit=1

 ID:                 66132
 Updated by:         [hidden email]
 Reported by:        utilmind at gmail dot com
 Summary:            X*5/X where X is floating point number is greater
                     than 5
-Status:             Open
+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:

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)


Previous Comments:
------------------------------------------------------------------------
[2013-11-21 00:19:36] [hidden email]

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

--
PHP-GTK Development Mailing List (http://gtk.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply | Threaded
Open this post in threaded view
|

Bug #66132 [Com]: X*5/X where X is floating point number is greater than 5

Guthrie at att dot net
In reply to this post by Guthrie at att dot net
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] [hidden email]

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] [hidden email]

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

--
PHP-GTK Development Mailing List (http://gtk.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php