As a matter of curiosity I\'ve been trying to determine at what point a 64-bit f
ID: 654452 • Letter: A
Question
As a matter of curiosity I've been trying to determine at what point a 64-bit float no longer reflects the addition of 1 as expected; that is, at what point the digits as printed do not correspond to the digits of a 64-bit integer that is incremented in sync.
Around 1e15 I can continually add 1 or subtract 1 and the result seems right. At 1e16 the addition or subtraction of 1 has no effect on the value as printed. However, 1e16 - 2 gives 9.999999999999998e+15 (at least, as printed by go's fmt package).
I wonder whether there are discontinuities in the sequence of sums of n+=1 while n < 1e15. At what point does n+=1 no longer produce a result corresponding to the expected integer?
Explanation / Answer
IEEE floating point format has a sign bit, an 11 bit exponent (ranging from -1022 to 1023) and a 52-bit mantissa with an implicit "1" in the 53rd bit.
Thus, the largest integer that can be represented without rounding is the binary number with 53 "1"s, 253?1 = 9,007,199,254,740,991 ~ 9e15 < 1e16. After that you start having to round off low order bits. So it looks like your experiment produced the right order of magnitude
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.