The floating-point format you are using has 53 bits in its significand. This means it can represent numbers as a 53-bit integer multiplied or divided by a power of two.
When 0.9999999999999999
is converted to this format, the closest representable value is (253−1)/253, which is exactly 0.99999999999999988897769753748434595763683319091796875. Note that the numerator uses exactly 53 bits.
When you add 1, the mathematical result would be (254−1)/253. The numerator would have 54 bits. This cannot be represented in the floating-point format, so it has to be rounded to fit. The two nearest representable values are:
- (254−2)/253 = (253−1)/252, and
- (254−0)/253 = 2.
These are both equally far away from the exact mathematically result. The rule for breaking ties is to use the value with a zero in the low bit of its significand. (Even though I have presented the significand as an integer here, it is a “left-adjusted” fraction in the floating-point format, so a numerator of 1 corresponds to the binary numeral 1.000000…0002.)
Thus, when 1 is added to 0.9999999999999999
, the computed result must be rounded to 2.