Why 0.1 + 0.1 == 0.2?
Asked Answered
M

1

8

This is concerning Java. From what I've understood, 0.1 cannot be perfectly represented by Java because of binary representations. That makes

0.1 + 0.1 + 0.1 == 0.3

false. However, why does

0.1 + 0.1 == 0.2

gives true?

Minimal answered 25/11, 2017 at 10:46 Comment(1)
Possible duplicate of Is floating point math broken?Cobweb
R
8

0.1 cannot be perfectly represented by Java because of binary representations. That makes

0.1 + 0.1 + 0.1 == 0.3

false.

That is not the entire reason why the equality is false, although it is part of it. 0.3 is not exactly 3/10 either. It so happens that 0.2 is exactly twice 0.1 (although they are not respectively 2/10 and 1/10), and that adding 0.1 to itself produces the value that is also the one you get when you type the constant 0.2. On the other hand, the overall approximation that you get after the operations 0.1 + 0.1 + 0.1 is slightly different of the approximation separating 0.3 from 3/10.

If we were using decimal with 5 significant digits, you might be surprised that 1 / 3 * 3 == 1 does not hold (1 / 3 would compute as 0.33333 and that times 3 would compute as 0.99999, which is different from 1), whereas 1 / 4 * 4 == 1 does hold (1 / 4 would compute as 0.25, and that times 4 would compute as 1).

Your question is somewhat similar to this, but for base-2 computations. Every constant and operation is an opportunity for an approximation. Sometimes the approximations do not happen, and sometimes they happen but cancel out, so that the end result is more accurate than you had a right to expect. In the case of 0.1 + 0.1, the result is not 2/10, but it is the same approximation of 2/10 that you get when you write 0.2, so that the equality holds. With 0.1 + 0.1 + 0.1 we happen not to be so lucky.

Redemption answered 25/11, 2017 at 11:3 Comment(3)
Yeah, true, I wrote kind of unconcisely but the 0.3 thing you said is what I meant. Thanks! So 0.1 + 0.1 == 0.2 is just "luck"? Or was there any greater reason that made it true?Minimal
@Minimal There is always a greater reason for anything that happens in binary floating-point: write out the binary digits and the operations cannot help but behaving the way they do. In this case there is a simple greater reason why 0.1 + 0.1 == 0.2: adding a number to itself, or multiplying by two, is always an exact operation in binary floating-point if overflow does not occur. So there is no approximation caused by the addition in 0.1 + 0.1. The result chosen for the addition is the nearest representable to 2/10 because it's exactly twice 0.1, which is the nearest to 1/10.Redemption
On the other hand, the operation 0.2 + 0.1 which happens as part of 0.1 + 0.1 + 0.1 is approximate (when writing out the binary digits, some rounding has to happen because the limitation to 53 significant binary digits does not allow to represent the ideal result).Redemption

© 2022 - 2024 — McMap. All rights reserved.