php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #64323 Float inconsistency between echo/var_dump and tests
Submitted: 2013-02-28 14:27 UTC Modified: 2013-02-28 15:18 UTC
From: guaycuru at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.4.12 OS: Any
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: guaycuru at gmail dot com
New email:
PHP Version: OS:

 

 [2013-02-28 14:27 UTC] guaycuru at gmail dot com
Description:
------------
So, I know that due to the way computers store float numbers, we end up with some 
oddities. This BUG is NOT about that. Instead, I found some inconsistencies on how 
PHP handle that, and it's easy to think of cases that those inconsistencies would 
end up causing lots of wasted hours debugging.

Please see test script, it should be pretty self explanatory.
My change request is that, since float / echo deal with that inconsistency 
(rounding 0.3000000000000000444089209850062616169452667236328125 to 0.3), so 
should control flow functions like IF, WHILE, etc...

Tested on PHP 5.3.22 and PHP 5.4.12

Test script:
---------------
<?php
$a = 0.1;
$b = 0.2;
var_dump($a + $b);
if($a + $b <= 0.3)
    echo "All is right in the universe!";
else
    echo "Something is wrong here!";
?>

Expected result:
----------------
float(0.3)
All is right in the universe!

Also, this result is acceptable:

float(0.3000000000000000444089209850062616169452667236328125)
Something is wrong here!

Actual result:
--------------
float(0.3)
Something is wrong here!

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-02-28 15:18 UTC] rasmus@php.net
You are getting the display precision confused with the internal representation 
of a float. You can change the display precision with ini_set('precision',32);
If you do that your output becomes:

float(0.30000000000000004440892098500626)
Something is wrong here!

It is just at the default display precision you end up with 0.3 in your case, 
but that has nothing to do with the internal representation of the float.
 [2013-02-28 15:18 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 21 17:01:36 2024 UTC