note 30071 modified in language.types.float by felipe
| From: | felipe@php.net | Date: | Sun, 09 Dec 2007 12:59:43 +0000 |
| Subject: | note 30071 modified in language.types.float by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-132976@lists.php.net to get a copy of this message | ||
I'd like to point out a "feature" of PHP's floating point support that
isn't made clear anywhere here, and was driving me insane.
This test (where var_dump says that $a=0.1 and $b=0.1)
if ($a>=$b) echo "blah!";
Will fail in some cases due to hidden precision (standard C problem, that PHP docs make no mention
of, so I assumed they had gotten rid of it). I should point out that I originally thought this was
an issue with the floats being stored as strings, so I forced them to be floats and they still
didn't get evaluated properly (probably 2 different problems there).
To fix, I had to do this horrible kludge (the equivelant of anyway):
if (round($a,3)>=round($b,3)) echo "blah!";
THIS works. Obviously even though var_dump says the variables are identical, and they SHOULD BE
identical (started at 0.01 and added 0.001 repeatedly), they're not. There's some hidden
precision there that was making me tear my hair out. Perhaps this should be added to the
documentation?
--was--
I'd like to point out a "feature" of PHP's floating point support that
isn't made clear anywhere here, and was driving me insane.
This test (where var_dump says that $a=0.1 and $b=0.1)
if ($a>=$b) then echo "blah!";
Will fail in some cases due to hidden precision (standard C problem, that PHP docs make no mention
of, so I assumed they had gotten rid of it). I should point out that I originally thought this was
an issue with the floats being stored as strings, so I forced them to be floats and they still
didn't get evaluated properly (probably 2 different problems there).
To fix, I had to do this horrible kludge (the equivelant of anyway):
if (round($a,3)>=round($b,3)) then echo "blah!";
THIS works. Obviously even though var_dump says the variables are identical, and they SHOULD BE
identical (started at 0.01 and added 0.001 repeatedly), they're not. There's some hidden
precision there that was making me tear my hair out. Perhaps this should be added to the
documentation?
http://php.net/manual/en/language.types.float.php