php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #2835 floor (8.28 * 100) = 8.27
Submitted: 1999-11-26 16:07 UTC Modified: 1999-11-26 18:46 UTC
From: aulbach at unter dot franken dot de Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 3.0.12 OS: Linux glibc
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: aulbach at unter dot franken dot de
New email:
PHP Version: OS:

 

 [1999-11-26 16:07 UTC] aulbach at unter dot franken dot de
<?

$b=8.28; 

echo $b*100; 
echo "<BR>";
echo floor($b*100); 

?>

Results in:

828
827

Of course this happens with many other numbers too. (17.08, 18.40, 8.20 ...)

Same Problem is with (int) or anything which seems to convert it to an integer. Even
printf("%d",$b*100); returns 819 not 820.

It is not fully understandable how this could happen - it seems to be a problem in the underlying math-lib?
In my opinion, a float of 828 should always result in integer 828, independent of the binary representation.

Fix in this case: Using round() instead.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1999-11-26 16:59 UTC] rasmus at cvs dot php dot net
Your mistake is that you think that floating point values are absolute.  They are not.  8.28 is not 8.28 but actually 8.27999999999999999999999999999 or 8.280000000000000000001
If you want to force a certain behaviour when rounding floating point numbers you will need to add a fuzz factor to compensate for the computer's inability to store absolute floating point numbers.
eg. $fuzz = 0.000000001; echo round(3/2 + $fuzz);
This would make sure that a x.5 value would always be rounded up, for example.
 [1999-11-26 18:46 UTC] aulbach at unter dot franken dot de
There are some things which I will say to this good
explanation:

1. This was of course clear for me, why this happens.
But, sorry, Ramus, I'm not sharing your opinion, that
you have to take care about rounding problems by yourself.
In my eyes this keeps an error: The function doesn't do,
what you expect and the results could be very ugly.
See down.

2. The float number was printed out as "828" and not as
"827.99999999999999999".
So there must be an explizit float conversion, which does
it "right".

3. Ok, I understand, why round(9.5) is not 10,
but every simple pocket calculator does this simple
thing "right".

[Or Oracle for example:
SQL> select trunc(8.28*100.0,0) from dual;

TRUNC(8.28*100,0)
-----------------
              828

mySQL of course too.]

4. For this business applications this behavior of the database
mow saved me my live, cause I could see, if my change in the program
works right: The resulting difference has been above 450
Deutschmark (about $ 200) - this happens when you
calculate about 400,000 operations with this small error.

If you make this little error every day over a
year you pay about $60,000 too much (or too less).



 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Sun Jun 14 10:00:01 2026 UTC