Answers · The mathematics underneath
What is the difference between the range and the precision of a float32?
In shortRange is the span of sizes a float32 can hold before it becomes infinity or zero; precision is how many significant digits it keeps, so nearby values stay distinguishable.
Range is the span of magnitudes a float32 can name: roughly to in normal form. Precision is how many significant digits it keeps at any point inside that span: about seven decimal digits, so two values a million apart in size are still told apart only to a relative gap of about one part in ten million. The two are separate because they are spent from separate fields. A float32 uses 32 bits: one for sign, eight for exponent, twenty-three for fraction. The exponent decides where the leading digit sits, and the fraction decides how many digits follow it. A number can be well inside the range and still have its smallest useful increment swallowed, because precision is a relative promise, not an absolute one.
- one bit of sign
- twenty-three stored fraction bits after the implicit leading 1
- eight stored exponent bits, normally 1 to 254
- the bias that makes the exponent range include negative powers
The exponent sets the range
The exponent field is the range budget. It stores in eight bits, with bias , so normal values use through , giving powers up to . At the top the significand is , which equals , not . Multiplying that by gives the largest finite float32.
The largest finite value is . The smallest positive normal value is . The ratio is about , and of that is , so the normal range covers about 76 orders of magnitude. That is the whole range budget, and it is why an overflow happens at a definite ceiling rather than by gradual loss of digits.
The fraction sets the precision
Precision is the fraction budget. The stored bits plus the implicit leading make significant binary bits. Converting to decimal, , so a float32 carries about decimal digits. Near the gap between neighbouring float32 values is .
At magnitude the gap is . At it is . The same seven digits are present in both cases; what changes is the size of the unit they are counting. That is why precision is called relative. It does not mean the float32 can resolve everywhere; it means it can resolve about one part in of whatever magnitude the exponent has selected.
The natural guess is that a number just past the maximum comes back as a slightly wrong large number, as though the format were rounding. It does not. Above the result is infinity. Once infinity is in a sum, the sum is infinity; infinity minus infinity is NaN, and NaN then spreads through every operation that touches it. That is a range failure, and it is abrupt. Precision failure looks completely different: at the gap is , so evaluates to . At the gap is , so adding to leaves . The second number was nowhere near the maximum, and it still lost the addition.
At the bottom end the behaviour is less abrupt but not more benign. Below the smallest normal value the format enters the subnormal range, where the implicit leading is dropped and the fraction supplies the value at a fixed exponent. The smallest positive subnormal is , about . Between and that value the number of significant bits falls away, and below the result is zero. Zero is a perfectly ordinary float32, so arithmetic continues; a gradient that has become zero cannot be told apart from a gradient that is genuinely zero.
Changing the format changes the split, not the distinction. In float16 the exponent is five bits and the fraction ten, so the largest finite value is , the smallest normal is , and the significant bits are , giving decimal digits. In float32 the corresponding numbers are , and digits. A fixed bit budget can be spent on exponent or fraction, and a format that is generous with one is often miserly with the other. Within a float32, the safe place to work is near one, where the gap is and neither edge is close.
Worth keeping
- A float32 has roughly seven significant decimal digits, not seven decimal places.
- Exceeding the maximum gives infinity; falling below the smallest subnormal gives zero.
- Precision loss is relative: the gap between neighbouring float32 values grows in proportion to magnitude.
Where this goes next
This question is one step of Keeping The Numbers In Range, a course that works through its subject from the beginning. The page that teaches this part of it is What A Machine Can Hold, and it reads the whole way through whether or not you have an account.
What an account changes is the practice: problems standing under every idea, marked with a reason rather than a tick, in a copy of the course that is yours.
Read that pageThe whole courseOther questions people ask about this
- Why does normalisation subtract the mean and divide by the standard deviation?Subtracting the average removes the offset and dividing by the spread removes the size, so the output has zero average and unit spread whatever scale the input had.
- Why does adding a layer's input to its output make very deep networks easier to train?Because the addition puts a derivative of exactly one on the path through it, so the gradient reaching the early layers is not a long product of small factors and does not shrink with depth.
- Why do gradients become tiny in the early layers of a deep network?Because the backward pass multiplies one number per layer, and a factor slightly below one, repeated over many layers, collapses towards nothing.
- What is the difference between variance and standard deviation?They measure the same spread: variance is the average squared distance from the mean, standard deviation is its square root, so you report the standard deviation and calculate with the variance.
- How do you make a vague claim precise enough to tell whether it's true?Fix what is being talked about, what is asserted, which cases are covered and the cut-off, then name an observation that would prove it false.
- How do you find the parabola that best fits a function near a point?Match the function's height, slope and bending at the chosen point; those three conditions single out exactly one parabola.