whu var_dump(0.1 + 0.2 == 0.3); return false

php https://code.sololearn.com/w6CnaZ0E3GF4/?ref=app

3/22/2019 9:02:02 PM

Gex Melk

6 Answers

New Answer


I'm no expert in PHP, but I'm guessing it has to do with floating-point values being approximations. So in other words: maybe 0.3 is actually stored as 0.30000000003.


Because float numbers have a specific accuracy of their representation. You can find more info here: http://php.net/manual/en/language.types.float.php Hope it helps you.




Follow this link: https://stackoverflow.com/q/588004/9166287


So in this case, what is the best way to compare for equality? a) multiplying each by 10 or 100 and casting to integers or b) checking if the sum on the left lies within a small tolerance of the value on the right hand side?