+ 7

Do all languages have this anomaly, 0.3 + 0.4 == 0.7 will be false ?

associativity is also killed in decimal operations (0.1 + 0.2) + 0.3 === 0.1 + ( 0.2 + 0.3) returns false in JavaScript. this issue also happens in java, c++ ! just caught this up while watching a video from Douglas Crockford, The Post JavaScript Apocalypse. ( good dinner time video)

12th Mar 2018, 12:58 PM
Morpheus
Morpheus - avatar
6 Answers
+ 5
could it have anytging to do with operator precedence, that == is evaluated first?
12th Mar 2018, 2:06 PM
storm
storm - avatar
+ 4
hmmm @immortal & morpheus. if that's the case (rounding problem) then it's a very "disturbing" problem. couldn't we go round it? let's say by transforming it to a number which doesn't have rounding errors? maybe type conversion to char/string and then evaluating for equality?
12th Mar 2018, 2:36 PM
storm
storm - avatar
+ 2
@storm No we can't go around the problem. What would we do with infinite numbers??? We can't store them as we don't have infinite data storage. So we have to round them. Still, the precision is quite good, usually 10^(-12) or less, so for normal calculations it suffices. If not enough, there are libraries or extensions that would give better precision out there. If you are interested in the precision of a certain number, languages usually have it stored in a variable or method called epsilon.
12th Mar 2018, 2:43 PM
spcan
spcan - avatar
+ 2
It's kind of how floats work
13th Sep 2018, 11:37 PM
👑 Prometheus 🇾🇬
👑 Prometheus 🇾🇬 - avatar
+ 1
@storm , nope, looks like it's the core issue in the old IEEE format we use in our number storage, as explained a bit by Immortal
12th Mar 2018, 2:07 PM
Morpheus
Morpheus - avatar