+ 6

[still unanswered] An easy but hard to understand question in programming languages.

0.1 + 0.2 == 0.3 output False?

13th Jan 2018, 11:29 PM
Tran Huu Dang
Tran Huu Dang - avatar
7 Answers
+ 7
in JavaScript 0.1 + 0.2 is 0.3000000004. That's make me confusing.
13th Jan 2018, 11:49 PM
Tran Huu Dang
Tran Huu Dang - avatar
+ 6
@nonzyro are you try 1.2 - 1 ?? it still less than 0.2
16th Jan 2018, 12:42 AM
Tran Huu Dang
Tran Huu Dang - avatar
+ 4
I'm not sure. It should be true.
13th Jan 2018, 11:37 PM
Robyn A
Robyn A - avatar
+ 3
Nope Julius, I suggested many things, reread my post. Here's one: # 0.1 + (0.2 == 0.3) // operator precedence # 0.1 + 0 // false # (bool) 0.1 // cast (implicit) # 0 // rounded when casting # 0 = false
24th Jan 2018, 12:19 AM
non
+ 2
It might be the floating point problem. Computer only stores approximate values for floating point numbers, so sometimes after arithmetic numbers that should be equal are not. I'll leave it up to someone else to post links. Edit: whatever, I can't sleep anyway. I'm not JS developer, but the best I could find is this console.log(Math.abs(0.1 + 0.2 - 0.3)<0.00001) Yeah, I don't like it, too.
14th Jan 2018, 12:56 AM
BlazingMagpie
BlazingMagpie - avatar
+ 1
https://stackoverflow.com/questions/17404513/floating-point-equality-and-tolerances There's no absolute rule of how to do it correctly, just some possible ways.
16th Jan 2018, 8:51 AM
BlazingMagpie
BlazingMagpie - avatar
0
Wouldn't precedence make it 0.1 + 0 since == is higher? IDK js but other languages it is. Next: (bool) 0.1 is rounded when cast, thus it is 0. Again this is based on standard coding rules. The == is a bool so that's (bool) 0 added to float 0.1 which is cast to bool 0.1 which is 0. 0 + 0 = 0. Another consideration is 0.1 is cast to bool - thus 0 - by the calling function. Whichever, it should be False by my reckoning.
14th Jan 2018, 12:10 AM
non