php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37642 3 * 1.35 - 4.05 = 8.881784197E-16 but should be 0.0 (zero) (ALL versions!)
Submitted: 2006-05-30 09:41 UTC Modified: 2006-05-30 09:48 UTC
From: richard at besite dot nl Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.1.4 OS: Linux and Windows XP
Private report: No CVE-ID: None
 [2006-05-30 09:41 UTC] richard at besite dot nl
Description:
------------
The calculation of this simple piece goes wrong:
3 * 1.35 - 4.05
Under Linux it gives: 8.881784197E-16
Under Windows it gives: 8.8817841970013E-016

Tested under Linux with: PHP4 4.4.2-1+b1 (Debian package php4 4.4.2-1), PHP5 5.1.2-1+b1 (Debian package php5 5.1.2-1), PHP5 5.1.2 (Ubutu package php5 5.1.2-ubuntu3).
Tested under Windows with: PHP4 4.4.2, PHP5 5.1.4, PHP5 snapshot (5.2.0-dev of 05/30/2006), PHP6 snapshot (6.0.0 dev of 05/30/2006 8:30).

Btw, "3 * 1.36 - 4.08 == 0" returns true like you would expect.

Reproduce code:
---------------
<?php
var_dump( 3 * 1.35 - 4.05 == 0 );
print( 3 * 1.35 - 4.05 );
?>

Expected result:
----------------
bool(true)
0

Actual result:
--------------
bool(false)
8.881784197E-16

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-05-30 09:48 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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 27 06:01:31 2024 UTC