php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66132 X*5/X where X is floating point number is greater than 5
Submitted: 2013-11-20 23:53 UTC Modified: 2013-11-21 14:41 UTC
Votes:2
Avg. Score:4.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (50.0%)
From: utilmind at gmail dot com Assigned:
Status: Not a bug Package: PHP-GTK related
PHP Version: 5.4.22 OS: Any, tested in Win32 and CentOS
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: utilmind at gmail dot com
New email:
PHP Version: OS:

 

 [2013-11-20 23:53 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-11-21 00:19 UTC] scorninpc@php.net
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-21 14:41 UTC] bwoebi@php.net
-Status: Open +Status: Not a bug
 [2013-11-21 14:41 UTC] bwoebi@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.

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 15:07 UTC] utilmind at gmail dot com
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 14:01:30 2024 UTC