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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
34 - 28 = ?
Subscribe to this entry?

 
 [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

Add a Patch

Pull Requests

Add a Pull Request

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: Fri Apr 19 03:01:27 2024 UTC