php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64674 Calculation error with ceil
Submitted: 2013-04-19 09:26 UTC Modified: 2013-04-19 12:55 UTC
Votes:2
Avg. Score:2.0 ± 1.0
Reproduced:0 of 2 (0.0%)
From: guillaume dot loire at cnrs dot fr Assigned:
Status: Not a bug Package: Math related
PHP Version: Irrelevant OS: Linux debian Squeeze
Private report: No CVE-ID: None
 [2013-04-19 09:26 UTC] guillaume dot loire at cnrs dot fr
Description:
------------
Hello,
I found a calculation error with ceil function.
When doing a calculation such as this : ceil(8.80*1.00*100) it returns 881 instead of 880.
While doing ceil(880) it returns (as it should) 880.
I think this error occurs when running ceil function cumulative with calculation and not with a single number.

I wrote a little script to identify if others numbers are impacted and i found theses ones when running it from 10 to 10000, but there may be other.


Test script:
---------------
<?php
for ($i = 10; $i <= 10000; $i+=10) {
$toto=ceil(($i/100*1.00)*100);
if (preg_match( '/1$/',$toto)) {
echo $toto."<br>";
}
}
?>

Expected result:
----------------
110
220
440
490
830
880
930
980
1610
1660
1710
1760
1810
1860
1910
1960
2010
3220
3270
3320
3370
3420
3470
3520
3570
3620
3670
3720
3770
3820
3870
3920
3970
4020
4070
6440
6490
6540
6590
6640
6690
6740
6790
6840
6890
6940
6990
7040
7090
7140
7190
7240
7290
7340
7390
7440
7490
7540
7590
7640
7690
7740
7790
7840
7890
7940
7990
8040
8090
8140
8190

Actual result:
--------------
111
221
441
491
831
881
931
981
1611
1661
1711
1761
1811
1861
1911
1961
2011
3221
3271
3321
3371
3421
3471
3521
3571
3621
3671
3721
3771
3821
3871
3921
3971
4021
4071
6441
6491
6541
6591
6641
6691
6741
6791
6841
6891
6941
6991
7041
7091
7141
7191
7241
7291
7341
7391
7441
7491
7541
7591
7641
7691
7741
7791
7841
7891
7941
7991
8041
8091
8141
8191

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-04-19 10:45 UTC] phpmpan at mpan dot pl
The result is perfectly fine and expected. Please read about floating point numbers, operations on them and their usage.

Ceiling of 880.0000000000001136868377216160297393798828125, which is the result of multiplying 8.8 by 1.0 by 100, is in fact 881.

Also note that real numbers ceiling operation makes sense only for a tiny subset of all floating point numbers. For most of inputs there is either no result possible at all or relative difference between input and the result is too small to be considered important.
 [2013-04-19 12:55 UTC] nikic@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.


 [2013-04-19 12:55 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Dec 07 09:01:24 2019 UTC