php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26526 Unexpected behavior with ceil()
Submitted: 2003-12-04 15:16 UTC Modified: 2003-12-04 15:35 UTC
From: vince at blue-box dot net Assigned:
Status: Not a bug Package: Math related
PHP Version: 4CVS-2003-12-04 (stable) OS: FreeBSD 4.8-RELEASE-p13
Private report: No CVE-ID: None
 [2003-12-04 15:16 UTC] vince at blue-box dot net
Description:
------------
When using certain integers derived from multiplying integers and floating point numbers with ceil(), ceil() will increment the number even though it shouldn't be.

Reproduce code:
---------------
<?php

$value = 6*.029*1000;
echo "value = $value\n";

$value = ceil($value);
echo "value = $value\n";

?>

Expected result:
----------------
Since 6*.029*1000 comes out to be exactly 174, I would expect the output of the above script to be:

value = 174
value = 174

Actual result:
--------------
value = 174
value = 175

It seems like if this were a floating point precision problem the first echo statement in the code example would produce something like:

value = 174.000000000000000000001

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-12-04 15:35 UTC] sniper@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.
 
Thank you for your interest in PHP.

# cat t.php 
<?php

ini_set('precision', 20);
$value = 6*.029*1000;
echo "value = $value\n";

?>

# php t.php 
value = 174.00000000000002842

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Oct 11 03:01:27 2024 UTC