php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52324 bad result when adding two floats
Submitted: 2010-07-13 00:35 UTC Modified: 2010-07-13 10:55 UTC
From: brunog at micro-examples dot com Assigned:
Status: Duplicate Package: *General Issues
PHP Version: 5.2.14RC2 OS: ubuntu
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: brunog at micro-examples dot com
New email:
PHP Version: OS:

 

 [2010-07-13 00:35 UTC] brunog at micro-examples dot com
Description:
------------
PHP Version => 5.2.4-2ubuntu5.10
can you please consider this short code ?

Test script:
---------------
<?php
        $f1 = 0.09 ;
        $f2 = 0.01 ;
        $tot = $f1 + $f2 ;
        echo("$tot\n") ;
?>


Expected result:
----------------
0.1


Actual result:
--------------
0.0:

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-13 00:39 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2010-07-13 00:39 UTC] rasmus@php.net
prints 0.1 for me.  You are probably hitting a floating point limitation combined 
with a precision setting problem in your environment.  Try adding 
ini_set('precision',16); to the top of your script and you will see.
 [2010-07-13 00:58 UTC] brunog at micro-examples dot com
thank you for the quick reply
adding ini_set('precision',16); does not give correct result value :
0.09999999999999999

is it an echo() bug ?
0.0: is lexicographically before 0.1
0.09 + 0.01 = 0.0: is correct ascii saying
 [2010-07-13 01:04 UTC] rasmus@php.net
That is the "correct" value according to how computers store floating point 
values.  If you want it accurate to 2 decimal places, round() it to 2 decimal 
places.  Read the big red warning box here: 
http://php.net/manual/en/language.types.float.php
 [2010-07-13 01:10 UTC] brunog at micro-examples dot com
maybe, but 0.0: is not a correct value at all as an add result, whatever floating point accuracy is
0.09 + 0.02 gives 0.11 as expected, neither 0.109999999999 nor 0.::
 [2010-07-13 01:16 UTC] rasmus@php.net
It doesn't show 0.0 on any PHP version I have access to.  No idea about your 
5.2.4.  That's from 3 years ago.  I don't have anything that old on any of my 
machines.
 [2010-07-13 01:35 UTC] brunog at micro-examples dot com
strangely it works fine on an older php version 
PHP Version => 5.1.6
gives 0.1 as expected
 [2010-07-13 04:52 UTC] aharvey@php.net
-Status: Bogus +Status: Duplicate
 [2010-07-13 04:52 UTC] aharvey@php.net
Actually, the colon is part of the output here: we have a rare bug
that seems to be platform, architecture, compiler and optimisation
level dependent. (It may also involve the phase of the moon, but we
can't be absolutely sure -- I've never been able to reproduce it,
and I've tried pretty hard.)

Other bugs discussing this issue include bug #47716, bug #49764 and
bug #51396.
 [2010-07-13 10:55 UTC] brunog at micro-examples dot com
yes the issues you mention are connected to mine
since there are only a few operations in my source code example, and not billions as in others, maybe it will be easier to track the bug
to me, lunar phase is a stranger to this problem but I can check again in a few days if you like :))
I believe it is a conjonction between floating point internal representation and float to str routine that causes problem
I have this problem on CODERO and 1AND1 dedicated servers that come with ubuntu, so I think many other users may be impacted too
a workaround in my case would be to multiply operands by 100 and to divide result by 100 
it seems that this issues goes back and forth depending on multiple criteria, and may come again at any time
thank you
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC