whu var_dump(0.1 + 0.2 == 0.3); return false | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

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

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

22nd Mar 2019, 9:02 PM
Gex Melk
6 Answers
+ 10
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.
22nd Mar 2019, 9:12 PM
Division by Zero
+ 10
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.
22nd Mar 2019, 9:14 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
23rd Mar 2019, 1:22 AM
Adnan Zawad Toky
Adnan Zawad Toky - avatar
+ 3
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?
23rd Mar 2019, 3:47 AM
Sonic
Sonic - avatar
24th Mar 2019, 3:01 AM
Paolo De Nictolis
Paolo De Nictolis - avatar