note 30071 added to language.types.float
| From: | backov at spotbrokers-nospamplz dot com | Date: | Wed, 05 Mar 2003 19:16:44 +0000 |
| Subject: | note 30071 added to language.types.float | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-44891@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) 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://www.php.net/manual/en/language.types.float.php
http://master.php.net/manage/user-notes.php?action=edit+30071
http://master.php.net/manage/user-notes.php?action=delete+30071
http://master.php.net/manage/user-notes.php?action=reject+30071