php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49418 The greatest bug in the history of PHP, aka "I do not understand floats"
Submitted: 2009-08-30 17:29 UTC Modified: 2009-08-30 19:30 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: ilies dot radu at gmail dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.2.10 OS: MacOSX, NetBSD, Linux
Private report: No CVE-ID: None
 [2009-08-30 17:29 UTC] ilies dot radu at gmail dot com
Description:
------------
---- Example #1 / FAIL (This should NOT FAIL, but it does) !!!!!!!!!!!
$a=1.3333;
$b=1.6666;
$c=2.9999;
$d=($a+$b);
if($d != $c) {
	echo 'php have a serious bug ...';
} else {
	echo 'OK ...';
} //end if else
// notice that currently I use number_format($d, 4, '.', '') to fix this, but is abnormal !
----

---- Example #2 / OK
$a=4.3333;
$b=4.6666;
$c=8.9999;
$d=($a+$b);
if($d != $c) {
	echo 'php have a serious bug ...';
} else {
	echo 'OK ...';
} //end if
// this seems that not raise that bug, ... hmmm ... very strange
----


I tested it on:
PHP 4.4.9
PHP 5.2.9
PHP 5.2.10
PHP 4.4.4 (bundled with Zend Studio)
It appears that all fail at Example #1 and should not ... !!!

If you release a patch, please also patch the 4.4.9 version.

Reproduce code:
---------------
$a=1.3333;
$b=1.6666;
$c=2.9999;
$d=($a+$b);
if($d != $c) {
	echo 'php have a serious bug ...';
} else {
	echo 'OK ...';
} //end if else


Expected result:
----------------
the output should be 'OK ...'
but is not !

Actual result:
--------------
'php have a serious bug ...'

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-08-30 17:40 UTC] colder@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://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 01 14:01:29 2024 UTC