php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38321 Oddness and errors with float precision
Submitted: 2006-08-04 00:35 UTC Modified: 2006-08-04 21:25 UTC
From: ibexris at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.1.4 OS: Fedora Core
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: ibexris at gmail dot com
New email:
PHP Version: OS:

 

 [2006-08-04 00:35 UTC] ibexris at gmail dot com
Description:
------------
I just got bitten by a float precision problem and ceil() that affects several versions of php and definitely doesn't behave in a consistent manner.  I've tested this on linux (x86 and x86_64) and OSX with the same results, so I don't believe that it's dependent on the system libraries.  You'll also notice that the problem goes away by rounding the results below PHP's 14 digit float precision, and that it only affects certain numbers (which made this rather hard to track down).

Reproduce code:
---------------
<?php
   $x = 1.2 * 100;
   echo "$x\n", ceil($x), "\n";
   $x = 1.1 * 100;
   echo "$x\n",
        ceil($x), "\n",
        ceil(round($x, 13)), "\n",
        ceil(round($x, 14)), "\n";
?>


Expected result:
----------------
120
120
110
110
110
110


Actual result:
--------------
120
120
110
111
110
111


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-08-04 07:54 UTC] tony2001@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://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.


 [2006-08-04 08:13 UTC] ibexris at gmail dot com
My point was that if php displays the value as 110 when I print it out, it *should* be 110 when I run a function like ceil() on it.  If php thinks the value is 110.00000000000001, then that's what it should be printing when I echo.  I don't care so much that floats are inaccurate (I already knew that), but that php is giving me false information (saying 110 when the actual value is 110.00000000000001).
 [2006-08-04 20:34 UTC] ibexris at gmail dot com
Reopening this in case no one sees the comments for "bogus" bugs.  Close it again if you want, but I still consider it to be a valid bug -- the false information provided resulted in numerous financial errors in my code.
 [2006-08-04 21:25 UTC] tony2001@php.net
It has nothing to do with PHP itself, that's how computers work with floats.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Aug 14 07:00:03 2025 UTC