php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46272 invalid numbers returned
Submitted: 2008-10-10 18:39 UTC Modified: 2008-10-14 21:37 UTC
From: chris at corecoding dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.6 OS: Multiple: Debian and Mac OS X
Private report: No CVE-ID: None
 [2008-10-10 18:39 UTC] chris at corecoding dot com
Description:
------------
Both results should be 0. They are not!

Reproduce code:
---------------
$total = 2291.45;
$paid = 2453.425;
$tax = 161.975;

echo (($total - $paid) + $tax) . "\n";
# -3.69482222595E-13

echo (($total + $tax) - $paid) . "\n";
-4.54747350886E-13


Expected result:
----------------
0!

Actual result:
--------------
-3.69482222595E-13

and

-4.54747350886E-13





Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-10 18:41 UTC] felipe@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/


 [2008-10-14 21:37 UTC] johannes@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.

.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 22:01:31 2024 UTC