php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #6220 floor((0.7+0.1)*10) wrong
Submitted: 2000-08-17 12:23 UTC Modified: 2000-08-17 15:40 UTC
From: rockojfonzo at gmx dot net Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.0.1pl2 OS: MacOS X Server
Private report: No CVE-ID: None
 [2000-08-17 12:23 UTC] rockojfonzo at gmx dot net
with the argument "(0.7+0.1)*10" resulting in "8" floor() 
returns 7. Huh?
For other values it?s okay.

$result1=(((0.7+0.1)*10));
echo "Result 1 ((0.7+0.1)*10): $result1 = OK!<BR>";
$result2=floor( $result1);
echo "Result 2 (floor(result1)): $result2, should be 8, 
too<BR>";
$result3=floor((0.7+0.1)*10)/10;
echo "Result 3 (floor((0.7+0.1)*10)/10): $result3*";

./configure --with-mysql --with-apxs

It was the same with PHP3, that?s why I upgraded to 
Version 4.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-08-17 12:42 UTC] hholzgra@php.net
(and it's the same in C)

this is related to binary float 
representation in microprocessors

.7 and .1 are periodic fractions
in binary system so you get minimal 
rounding errors

so (0.7+0.1)*10) will result in something
almost 8.0 but with a minimal positive or negative
offset

this usualy dosn't matter as rounding will be
applied but it does matter for floor() as in this
case the result is a very little less than 8

conclusion: if you need the precisision than use bc
 [2000-08-17 14:13 UTC] waldschrott@php.net
I think we should document what is "precision" and when
those problems arise and what to do to prevent them...

most people will think that they don?t need arbitrary
precision for trivial things like 0.7+0.1
 [2000-08-17 15:28 UTC] hholzgra@php.net
done
 [2000-08-17 15:40 UTC] waldschrott@php.net
where did you add it to? function floor() is not the only
place where it?d be needed...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 11:01:31 2024 UTC