5

If I iterate over binary representations from 000...000 to 111...111, is there a significant transition at some point?

In the IEEE 754 float32 format, the position of the bits matters, where the most significant bit (MSB) is the sign bit:

sign (1) | exponent (8) | mantissa (23)

If I start iterating from 0, 1, 2, 3 in binary representation and convert each iteration to its float32 representation, I observe the smallest possible transition (or step) between consecutive float32 values, which is expected.

However, I am uncertain about what happens during larger iterations, specifically when the exponent and sign bits are affected.

So, for example, the binary of 100110..0001 might has big difference in the perspective of float32 like 1.222 to the25.2 if the binary is incremented.

So, my question is: at what points does incrementing a binary number result in a large difference in its corresponding float32 value?"

2 Answers2

15

Incrementing 011111110111...111 to 011111111000...000 changes the float32 value from $\approx3{.}4\times10^{38}$ to +infinity, which is an infinite difference. Likewise with negative sign. (There are also transitions to/from NaNs, but for those the magnitude of the “difference” is meaningless, I’d say.)

The largest finite difference, $\approx2{.}028\times10^{31}$, occurs between any two consecutive numbers of the form (0/1)11111110... (with absolute values between $\approx1{.}7\times10^{38}$ and $\approx3{.}4\times10^{38}$).

Emil Jeřábek
  • 3,668
  • 17
  • 20
8

For all of the "normal" finite floating point numbers (those between 1.18*10^-38 and 3.40*10^38, or between -1.18*10^-38 and -3.40*10^38, for IEEE-754 binary32 floats), the behavior is very regular: the step between two successive floats is constant between any two powers of two, and doubles at each power of two.

That is:

  • For all of the floats in [0.5, 1), incrementing the binary representation by 1 will increase the floating-point value by 2^-24.
  • If a value is in [1, 2), incrementing its binary representation will increase it by 2^-23.
  • If a value is in [2, 4), incrementing its binary representation will increase it by 2^-22.
  • ...
  • If a value is between 2^23 and 2^24, incrementing its binary representation will increase it by 1.
  • et cetera, up to the largest finite value.

For the subnormals and zeroes (values between ±1.18*10^-38), it's also simple: the step is 2^-149 for all of them, and doesn't change at powers of two.

There are no unexpected "jumps" within the finite values. The largest finite values are adjacent to ±Inf, which in turn are adjacent to NaNs, but it doesn't make any sense to talk about the numerical difference between those.

As a special case, +0 (0x00000000) and -0 (0x80000000) are also adjacent to NaNs (0xFFFFFFFF and 0x7FFFFFFF, respectively).

hobbs
  • 419
  • 2
  • 6