First, let’s restate the setup more explicitly:
You start with a single-precision (float) variablex=0.0fx = 0.0fx=0.0f
You repeatedly dox=nextafterf(x,FLT_MAX)x = \text{nextafterf}(x, \text{FLT_MAX})x=nextafterf(x, means: “give me the next representable float afterxxxin the direction ofFLT_MAX\text{FLT_MAX}FLT_MAX.”
Empirically, you observe that for quite a long stretch, each increment is the same “tiny” step, leading to a near-linear plotx(n)=const×n.x(n) ;=; \text{const} \times n.x(n)=const×n.You want to know:What is the value of that const?How long does it hold before it breaks (i.e. spacing changes)?
What is the value of that const?
How long does it hold before it breaks (i.e. spacing changes)?
You start with a single-precision (float) variable
You repeatedly do
which means: “give me the next representable float afterxxxin the direction ofFLT_MAX\text{FLT_MAX}FLT_MAX.”
Empirically, you observe that for quite a long stretch, each increment is the same “tiny” step, leading to a near-linear plot
You want to know:
What is the value of that const?
How long does it hold before it breaks (i.e. spacing changes)?
What is the value of that const?
How long does it hold before it breaks (i.e. spacing changes)?
- The “const” in single-precision floats
Smallest step above 0 (subnormal increment)
In IEEE 754 single-precision (32-bit) floats:
Exponent: 8 bits (biased by 127)
Fraction (mantissa): 23 bits stored
One “implicit leading 1” in normal numbers
Exponent: 8 bits (biased by 127)
Fraction (mantissa): 23 bits stored
One “implicit leading 1” in normal numbers
However, floats also have asubnormal (denormal)range for very small magnitudes, in which the leading bit is effectively 0 and the exponent is fixed to the smallest possible value (all exponent bits zero, which is biased exponent = 1−127 = −126, but forced leading bit = 0).
In that subnormal region, the spacing between adjacent floats is
Concretely,
This is the smallest positive increment (the gap) you get when you donextafterf(0.0f, FLT_MAX)starting from zero and remain in the subnormal region.
Hence:
const=2−149≈1.×10−45=; 2^{-149} ;\approx; 1.\times 10^{-45}=2−149≈1.×10−45.
- How long does that spacing remain constant?
Surprisingly,that same 2⁻¹⁴⁹ incrementcontinuesbeyondjust the “subnormal” floats, because once you reach the smallest normal float (1.0 × 2⁻¹²⁶), each increment in the fraction bits at exponent = is still2−232^{-23}2−23of2−1262^{-126}2−126, i.e. again2−1492^{-149}2−149.
Let’s outline it carefully:
Subnormal floats:These go from the smallestpositivesubnormal2−1492^{-149}2−149up through the largest subnormal(1−2−23)×2−126\bigl(1 - 2^{-23}\bigr)\times 2^{-126}(1−2−23)×2−126.At each step, you keep adding2−1492^{-149}2−149.
These go from the smallestpositivesubnormal2−1492^{-149}2−149up through the largest subnormal(1−2−23)×2−126\bigl(1 - 2^{-23}\bigr)\times 2^{-126}(1−2−23)×2−126.
At each step, you keep adding2−1492^{-149}2−149.
Normal floats (exponent = −126):The next float after the largest subnormal is1.0×2−126=2−126.1.0 \times 2^{-126} ;=; 2^{-126}.1.0×2−126=2−126.Then as you increment fraction bits at exponent = −126, the spacing is still2−23×2−126=2−149.2^{-23} \times 2^{-126} ;=; 2^{-149}.2−23×2−126=2−149.
The next float after the largest subnormal is1.0×2−126=2−126.1.0 \times 2^{-126} ;=; 2^{-126}.1.0×2−126=2−126.
Then as you increment fraction bits at exponent = −126, the spacing is still2−23×2−126=2−149.2^{-23} \times 2^{-126} ;=; 2^{-149}.2−23×2−126=2−149.
Subnormal floats:
These go from the smallestpositivesubnormal2−1492^{-149}2−149up through the largest subnormal(1−2−23)×2−126\bigl(1 - 2^{-23}\bigr)\times 2^{-126}(1−2−23)×2−126.
At each step, you keep adding2−1492^{-149}2−149.
These go from the smallestpositivesubnormal2−1492^{-149}2−149up through the largest subnormal(1−2−23)×2−126\bigl(1 - 2^{-23}\bigr)\times 2^{-126}(1−2−23)×2−126.
At each step, you keep adding2−1492^{-149}2−149.
Normal floats (exponent = ):
The next float after the largest subnormal is1.0×2−126=2−126.1.0 \times 2^{-126} ;=; 2^{-126}.1.0×2−126=2−126.
Then as you increment fraction bits at exponent = −126, the spacing is still2−23×2−126=2−149.2^{-23} \times 2^{-126} ;=; 2^{-149}.2−23×2−126=2−149.
The next float after the largest subnormal is
Then as you increment fraction bits at exponent = −126, the spacing is still
Not until you finally exhaustallfraction bits at exponent = (i.e. you move to exponent = −125) do you see the spacing jump to
which istwiceas large.
The magic 2^24 transition
Since single-precision floats effectively have24 bits of precision(23 bits stored + 1 implicit leading bit for normal numbers), you can count how many steps it takes before the exponent bumps from −126 to −125.
Fromx=0x=0x=0up to (but not including) exponent = −125, you have:223−12^{23} - 1223−1subnormal floats, plus2232^{23}223normal floats for exponent = −126.
223−12^{23} - 1223−1subnormal floats, plus
2232^{23}223normal floats for exponent = −126.
Fromx=0x=0x=0up to (but not including) exponent = −125, you have:
223−12^{23} - 1223−1subnormal floats, plus
2232^{23}223normal floats for exponent = −126.
223−12^{23} - 1223−1subnormal floats, plus
2232^{23}223normal floats for exponent = −126.
In a more direct sense, each increment isΔ=2−149\Delta = 2^{-149}Δ=2−149. When you have taken2242^{24}224such steps,
At that point, you have moved from near 0 up into the domain where the exponent is about to switch from −126 to −125, doubling the gap.
Hence you get a perfect linear relationshipx(n)=n×2−149x(n) = n \times 2^{-149}x(n)=n×2−149for
After224≈16.7million2^{24}\approx 16.7\text{ million}224≈16.7millionincrements, the spacing changes from2−1492^{-149}2−149to2−1482^{-148}2−148.
Final Answers
Value of “const”:const=2−149≈1.×10−45.\text{const} = 2^{-149}
;\approx; 1. \times 10^{-45}.const=2−149≈1.×10−45.
How long until it breaks (i.e. spacing changes)?Exactly for the (about 16.7 million increments).At that point, to2−1252^{-125}2−125and the gap doubles to2−1482^{-148}2−148.
Exactly for the (about 16.7 million increments).
At that point, to2−1252^{-125}2−125and the gap doubles to2−1482^{-148}2−148.
Value of “const”:
How long until it breaks (i.e. spacing changes)?
Exactly for the (about 16.7 million increments).
At that point, to2−1252^{-125}2−125and the gap doubles to2−1482^{-148}2−148.
Exactly for the (about 16.7 million increments).
At that point, to2−1252^{-125}2−125and the gap doubles to2−1482^{-148}2−148.