+ 6
[still unanswered] An easy but hard to understand question in programming languages.
0.1 + 0.2 == 0.3 output False?
7 Antworten
+ 7
in JavaScript 0.1 + 0.2 is 0.3000000004. That's make me confusing.
+ 6
@nonzyro are you try 1.2 - 1 ?? it still less than 0.2
+ 4
I'm not sure. It should be true.
+ 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
+ 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.
+ 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.
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.