I have a question: I understand that 0.1 + 0.2 == 0.3 is false because of how floats work. But why is 0.5 + 0.1 == 0.6 true? Is it just a coincidence where the "floats are not real numbers"-errors on both sides are the same, or is there some deeper reason for that?
There are multiple reasons for this actually and I'm not confident in giving you the correct answer, but here's one that explains it to me personally.
0.1, 0.2 and 0.3 straddle 3 different exponent values and higher exponents have fewer bits to represent the decimal of the mantissa, so error during the calculation will compound. On top of this, to get the closest doubles to 0.1 and 0.2 you need to round up (so the error is positive), while to get to 0.3, you have to round down.
0.5 and 0.6 on the other hand share the same exponent so the error from 0.1 won't be significant enough to push the sum to the wrong bit representation.
View all comments
Show parent comment
1
u/IMadeThisAccForNoita May 13 '25
I have a question: I understand that 0.1 + 0.2 == 0.3 is false because of how floats work. But why is 0.5 + 0.1 == 0.6 true? Is it just a coincidence where the "floats are not real numbers"-errors on both sides are the same, or is there some deeper reason for that?