|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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!
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 09 06:00:01 2025 UTC |
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.