HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

When does the IEEE-754 64-bit float break as a counter

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
thebitfloatieeedoeswhencounterbreak754

Problem

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?

Solution

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, $2^{53}-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.

Context

StackExchange Computer Science Q#20224, answer score: 5

Revisions (0)

No revisions yet.