php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60557 ceil function bug
Submitted: 2011-12-19 03:38 UTC Modified: 2012-01-03 12:24 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: vividy163 at hotmail dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.3.8 OS: Linux
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: vividy163 at hotmail dot com
New email:
PHP Version: OS:

 

 [2011-12-19 03:38 UTC] vividy163 at hotmail dot com
Description:
------------
<?php
echo ceil(12000*(7/100));
?>
it give me result 841.

Test script:
---------------
<?php
echo ceil(12000*(7/100));
?>
it give me result 841.

Expected result:
----------------
840

Actual result:
--------------
841

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-12-20 00:55 UTC] anon at anon dot anon
Ceil is correct. Due to the underlying limitations of the floating point math, 7/100 is not a fraction which can be exactly represented, so the CPU chooses the closest possible value, and after multiplying the result is: 840.0000000000001136868377216160297393798828125

All programming languages implementing IEEE 754 (see: https://en.wikipedia.org/wiki/IEEE_754-1985 ) (which is most of them) will give 841 for this computation. In this specific case, just do the multiplication first (remove the extra brackets):

echo ceil(12000*7/100); // outputs: 840
 [2011-12-20 01:01 UTC] anon at anon dot anon
(And if you need a solution for the more general case, consider subtracting a tiny value just before calling ceil:)

echo ceil((12000*(7/100)) - 1e-8); // outputs: 840
 [2012-01-03 12:24 UTC] mike@php.net
-Status: Open +Status: Bogus
 [2012-01-03 12:24 UTC] mike@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 Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 11:01:30 2024 UTC