Bug #60557 [Opn->Bgs]: ceil function bug
| From: | mike@php.net | Date: | Tue, 03 Jan 2012 12:24:28 +0000 |
| Subject: | Bug #60557 [Opn->Bgs]: ceil function bug | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-166178@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=60557&edit=1
ID: 60557
Updated by: mike@php.net
Reported by: vividy163 at hotmail dot com
Summary: ceil function bug
-Status: Open
+Status: Bogus
Type: Bug
Package: Math related
Operating System: Linux
PHP Version: 5.3.8
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2011-12-20 01:01:49] 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
------------------------------------------------------------------------
[2011-12-20 00:55:27] 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-19 03:38:37] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=60557&edit=1