Skip to content

15.8 Problem set A: representation, rounding, and cancellation

A bigram that computes its loss in float64 numpy on tiny logits never meets a numerical problem. A trained model does. Its logits reach the hundreds, so exp overflows float32 at about 88.788.7 and the naive softmax returns NaN. Its weights are stored in bf16 (the SmolLM2 checkpoint loaded in The Llama-family model: config, weight loading, and download in AI Engineering is), so rounding must match PyTorch exactly or parity checks fail in the last bit. Its evaluation sums 10710^7 per-token losses (The evaluation harness and the model zoo), and a float32 accumulator silently stops growing.

Concepts from other sections (16)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

IEEE 754 anatomy, ulps, and round to nearest even emulates bf16 and fp16 rounding, and Stable numerics writes the stable reductions every later operation calls. This pen-and-paper set makes sure you can predict each of those failures by hand before you read the code that avoids it. It covers representation and rounding (q1 to q5) and cancellation and stable rewrites (q6 to q11, ending with a short proof). Problem set B continues with error bounds, Newton’s method, polynomial approximation, and low precision.

The proof in q11 uses the habits of Discrete math 1 problem set, and q10 relies on the fact, from Vectors, matrices, row-major layout, and matmul, that float sums depend on their order. Work each problem before opening its answer. Each answer gives the final value, the derivation, and the common wrong answer with the reason it is wrong. Exact values are written as powers of two or fractions.

Learning objectives

  • Decompose a number into float32 fields and compute the gap between neighbouring floats in float32, bf16, and fp16.
  • Round exact ties to even in bf16 and recognise which decimal values are not representable in binary.
  • Predict when cancellation, overflow, or absorption in a running sum destroys a result, and rewrite the formula to avoid it.
  • Prove that softmax is shift invariant and that the max shift prevents overflow.

These are the symbols used in the problems. Note that here pp counts stored fraction bits, so the unit roundoff is 2−(p+1)2^{-(p+1)}.

SymbolMeaningType / shape
s,e,fs, e, fsign bit, biased exponent field, fraction fieldunsigned integers
ppfraction bits: 23 (float32), 10 (fp16), 7 (bf16)integer
bias127 (float32, bf16), 15 (fp16)integer
fl(x)\mathrm{fl}(x)xx rounded to the nearest representable valuefloat
ulp(x)\mathrm{ulp}(x)gap between the two floats around xxfloat
uuunit roundoff, 2−(p+1)2^{-(p+1)}: the largest relative rounding errorfloat
LSE(x)\mathrm{LSE}(x)log⁡∑iexi\log \sum_i e^{x_i}scalar

A normal binary float with pp fraction bits represents

x=(−1)s⋅(1+f2p)⋅2 e−bias,1≤e≤2w−2,x = (-1)^s \cdot \left(1 + \frac{f}{2^p}\right) \cdot 2^{\,e - \text{bias}}, \qquad 1 \le e \le 2^{w} - 2,

where ww is the exponent width. The leading 1 is implicit (not stored). The exponent field e=0e = 0 holds zero and the subnormals 0.f⋅21−bias0.f \cdot 2^{1 - \text{bias}}, which fill the gap below the smallest normal 21−bias2^{1 - \text{bias}}; e=2w−1e = 2^w - 1 holds ±∞\pm\infty (f=0f = 0) and NaN (f≠0f \ne 0). To decompose a number, write it as ±1.something×2E\pm 1.\text{something} \times 2^E in binary: −6.5=−110.12=−1.1012×22-6.5 = -110.1_2 = -1.101_2 \times 2^2, so s=1s = 1, e=2+127=129e = 2 + 127 = 129, and the fraction bits are 101101 followed by 20 zeros, the integer 5⋅2205 \cdot 2^{20}. The three formats compare as follows:

FormatExponent bitsFraction bits ppBiasGap at 1 (2−p2^{-p})Largest finite
float328231272−232^{-23}(2−2−23)⋅2127(2 - 2^{-23}) \cdot 2^{127}
bf16871272−72^{-7}(2−2−7)⋅2127(2 - 2^{-7}) \cdot 2^{127}
fp16510152−102^{-10}(2−2−10)⋅215(2 - 2^{-10}) \cdot 2^{15}

bf16 is the top 16 bits of a float32, so converting is a rounding of the low 16 bits. Within one binade [2E,2E+1)[2^E, 2^{E+1}) the floats are equally spaced with gap ulp=2E−p\mathrm{ulp} = 2^{E - p}, and the gap doubles at each power of two: the integers above 2242^{24} are not all float32 values, and above 10810^8 float32’s gap is 8.

Every arithmetic result is rounded to a representable value. Round to nearest, ties to even (RNE), the IEEE 754 default, picks the nearest float and, when the exact result is halfway between two, the one whose last fraction bit is 0. Ties to even avoids the upward drift that “round half up” would add over many operations. The relative error of one rounding is at most the unit roundoff u=2−(p+1)u = 2^{-(p+1)}, half the gap at 1: fl(x)=x(1+δ)\mathrm{fl}(x) = x(1 + \delta) with ∣δ∣≤u\lvert \delta \rvert \le u. Decimal fractions such as 0.10.1 have infinite binary expansions, because their denominator has the factor 5, so they are always rounded.

Subtraction of two floats is exact when they are within a factor 2 of each other (Sterbenz), so the damage is never the subtraction itself. It is that the operands already carry rounding errors from earlier steps, and subtracting nearly equal numbers cancels their shared leading digits and leaves those errors as the whole result. 108+1−108\sqrt{10^8 + 1} - \sqrt{10^8} in float32 is 0 because 108+110^8 + 1 already rounded to 10810^8. The cure is an algebraically equal form that does not subtract nearly equal quantities: multiply by the conjugate, x+1−x=1x+1+x\sqrt{x+1} - \sqrt{x} = \frac{1}{\sqrt{x+1} + \sqrt{x}}; use 1−cos⁡x=2sin⁡2(x/2)1 - \cos x = 2\sin^2(x/2); compute a variance from deviations rather than from E[x2]−E[x]2E[x^2] - E[x]^2.

exe^x overflows float32 above ln⁡(3.4×1038)≈88.7\ln(3.4 \times 10^{38}) \approx 88.7 and float64 above about 709.8709.8, and ∞/∞\infty/\infty is NaN. Softmax and log-sum-exp are unchanged by subtracting a constant from every input (q11), so subtracting m=max⁡ixim = \max_i x_i keeps every exponent at most 0:

LSE(x)=m+log⁡∑iexi−m,softmax(x)i=exi−m∑jexj−m.\mathrm{LSE}(x) = m + \log \sum_i e^{x_i - m}, \qquad \mathrm{softmax}(x)_i = \frac{e^{x_i - m}}{\sum_j e^{x_j - m}} .

Adding a small number to a large running sum rounds the small number away when it is below half a gap: in float32, 1+10−8=11 + 10^{-8} = 1 because the gap at 1 is 2−23≈1.2×10−72^{-23} \approx 1.2 \times 10^{-7}. A long sum then stops growing. Kahan summation carries a compensation term that captures the low-order bits each addition drops and feeds them back; pairwise summation adds in a balanced tree so that every partial sum is of similar size. Stable numerics implements both.

Before the problems, here are two siblings of q1 and q6, worked in full.

First, decompose 0.156250.15625 as float32. 0.15625=5/32=1012×2−5=1.012×2−30.15625 = 5/32 = 101_2 \times 2^{-5} = 1.01_2 \times 2^{-3}. The sign is s=0s = 0, the exponent e=−3+127=124e = -3 + 127 = 124, and the fraction bits are 0101 followed by 21 zeros, so f=221=2097152f = 2^{21} = 2097152. The answer is [0, 124, 2097152]. Check: (1+221/223)⋅2−3=1.25/8=0.15625(1 + 2^{21}/2^{23}) \cdot 2^{-3} = 1.25/8 = 0.15625.

Second, a cancellation. Evaluate g(x)=(1+x)−1g(x) = (1 + x) - 1 in float32 at x=3⋅10−8x = 3 \cdot 10^{-8}. The gap at 1 is 2−23≈1.19×10−72^{-23} \approx 1.19 \times 10^{-7}, so 1+x1 + x lies between 11 and 1+2−231 + 2^{-23}, closer to 1 (since x<2−24≈5.96×10−8x < 2^{-24} \approx 5.96 \times 10^{-8}), and rounds to 1. Then 1−1=01 - 1 = 0: the answer is 00, a 100% relative error, although the true value is 3⋅10−83 \cdot 10^{-8}. Algebra gives the stable form g(x)=xg(x) = x. The same structure, a sum that has already rounded away the part you are about to subtract back out, is q6.

A float32 has 1 sign bit, 8 exponent bits (bias 127), and 23 fraction bits; float16 (fp16) has 1, 5 (bias 15), and 10; bfloat16 (bf16) has 1, 8 (bias 127), and 7. The tag after each question is its answer type.

q1. Give the float32 fields [sign,biased exponent,fraction][\text{sign}, \text{biased exponent}, \text{fraction}], the fraction as the unsigned integer stored in its 23 bits, for (a) 1.01.0 and (b) −6.5-6.5. [vector]

Answer

(a) [0,127,0][0, 127, 0]. 1.0=+1.02×201.0 = +1.0_2 \times 2^0: sign 0, biased exponent 0+127=1270 + 127 = 127, and the fraction after the implicit 1 is all zeros.

(b) [1,129,5242880][1, 129, 5242880], where 5242880=5⋅2205242880 = 5 \cdot 2^{20}. −6.5=−110.12=−1.1012×22-6.5 = -110.1_2 = -1.101_2 \times 2^2: sign 1, exponent 2+127=1292 + 127 = 129, and the 23 fraction bits are 101101 followed by 20 zeros, the integer 1012⋅220=5⋅220101_2 \cdot 2^{20} = 5 \cdot 2^{20}.

A common mistake is [0,0,0][0, 0, 0] or [1,2,…][1, 2, \ldots] for the exponent, which forgets the bias; another is a fraction of 1 for 1.01.0, which stores the implicit leading 1 (it is never stored). Reading the fraction bits .101.101 as the decimal 5 gives [1,129,5][1, 129, 5], which ignores their position in the 23-bit field.

q2. Give the gap between 11 and the next larger representable number in (a) float32, (b) bf16, (c) fp16. [number]

Answer

(a) 2−232^{-23}, (b) 2−7=1/1282^{-7} = 1/128, (c) 2−102^{-10}. In the binade [1,2)[1, 2) the floats are 1+k⋅2−p1 + k \cdot 2^{-p} with pp the number of stored fraction bits, so the next float after 1 is 1+2−p1 + 2^{-p}.

A common mistake is 2−242^{-24} for float32: that is the unit roundoff uu, half the gap. For bf16, 2−82^{-8} counts the implicit bit as stored (bf16 has 8 significant bits but 7 stored fraction bits), and 2−102^{-10} is fp16’s gap, not bf16’s.

q3. (a) Give the largest finite fp16 value. (b) The smallest positive normal float32 value is 2k2^k; give kk. [number]

Answer

(a) 65504=(2−2−10)⋅21565504 = (2 - 2^{-10}) \cdot 2^{15}. The largest exponent field that is not infinity/NaN is 25−2=302^5 - 2 = 30, so the exponent is 30−15=1530 - 15 = 15, and the largest significand is all ones, 2−2−102 - 2^{-10}. A common mistake is 65536=21665536 = 2^{16}, which would need exponent 16 (that field is reserved for infinity), or 6553565535, the largest uint16.

(b) k=−126k = -126. The smallest normal exponent field is 1, so the exponent is 1−127=−1261 - 127 = -126 with significand 1. A common mistake is −127-127, which uses the field value 0 (reserved for zero and subnormals); −149-149 is the smallest subnormal, 2−126⋅2−232^{-126} \cdot 2^{-23}.

q4. Round to bf16 with RNE: (a) 1+2−81 + 2^{-8}, (b) 1+3⋅2−81 + 3 \cdot 2^{-8}. [number]

Answer

(a) 11. bf16’s gap at 1 is 2−72^{-7}, so the neighbours are 11 (fraction 0000000) and 1+2−71 + 2^{-7} (fraction 0000001). 1+2−81 + 2^{-8} is exactly halfway; ties to even picks the neighbour whose last bit is 0, which is 1. A common mistake is 1+2−71 + 2^{-7}, rounding the tie up.

(b) 1+2−6=65/641 + 2^{-6} = 65/64. 1+3⋅2−8=1+1.5⋅2−71 + 3 \cdot 2^{-8} = 1 + 1.5 \cdot 2^{-7} is halfway between 1+2−71 + 2^{-7} (fraction 0000001, odd) and 1+2−61 + 2^{-6} (fraction 0000010, even), so it rounds up to the even one. Answering 1+2−71 + 2^{-7} truncates or rounds ties down; ties to even goes up here and down in (a).

q5. Is 0.10.1 exactly representable in any binary floating-point format? [bool]

Answer

False. Every binary float is m/2km / 2^k for integers m,km, k (or m⋅2km \cdot 2^k). 0.1=1/10=1/(2⋅5)0.1 = 1/10 = 1/(2 \cdot 5) in lowest terms has the factor 5 in its denominator, which no power of two can cancel, so its binary expansion 0.00011‾20.0\overline{0011}_2 never terminates. Every format rounds it.

q6. Let f(x)=x+1−xf(x) = \sqrt{x + 1} - \sqrt{x} and x=108x = 10^8. (a) Evaluate f(x)f(x) in float32 with every operation rounded to float32 (RNE). What does it return? [number] (b) Give the true value of f(108)f(10^8), exactly or to at least 7 significant digits. [number, rtol 1e-6]

Answer

(a) 00. 10810^8 lies in [226,227)[2^{26}, 2^{27}), where the float32 gap is 226−23=82^{26 - 23} = 8. 108=28⋅39062510^8 = 2^8 \cdot 390625 is representable, and 108+110^8 + 1 is within half a gap of it, so it rounds to 10810^8. Both square roots are then 108=104\sqrt{10^8} = 10^4, and the subtraction gives 0.

(b) 1100000001+10000≈4.99999998750×10−5\dfrac{1}{\sqrt{100000001} + 10000} \approx 4.99999998750 \times 10^{-5}. Multiply by the conjugate: x+1−x=(x+1)−xx+1+x=1x+1+x\sqrt{x+1} - \sqrt{x} = \frac{(x + 1) - x}{\sqrt{x+1} + \sqrt{x}} = \frac{1}{\sqrt{x+1} + \sqrt{x}}, which subtracts nothing and is accurate in float32 too. A common mistake for (a) is to give this true value (≈1/20000\approx 1/20000) instead of what float32 computes. For (b), 1/100001/10000 is 1/x1/\sqrt{x}; the denominator is the sum of two roots, about 2x2\sqrt{x}.

q7. Which formula computes 1−cos⁡x1 - \cos x accurately in float64 at x=10−8x = 10^{-8}? [choice] (a) 1 - cos(x) (b) 2 * sin(x / 2)^2

Answer

(b). cos⁡(10−8)=1−5×10−17+…\cos(10^{-8}) = 1 - 5 \times 10^{-17} + \ldots, and the float64 gap just below 1 is 2−53≈1.1×10−162^{-53} \approx 1.1 \times 10^{-16}, so cos⁡x\cos x rounds to exactly 1 and (a) returns 0. The identity 1−cos⁡x=2sin⁡2(x/2)1 - \cos x = 2\sin^2(x/2) computes 2⋅(5×10−9)2=5×10−172 \cdot (5 \times 10^{-9})^2 = 5 \times 10^{-17} from a small number that was never subtracted from 1, so every digit is correct.

q8. (a) Give LSE(1000,1000)=log⁡(e1000+e1000)\mathrm{LSE}(1000, 1000) = \log(e^{1000} + e^{1000}) exactly. [number] (b) In float64, what does the naive softmax exp(x) / sum(exp(x)) return for its first entry at x=(1000,1000)x = (1000, 1000)? Answer one of 0.5, inf, nan, 0. [choice]

Answer

(a) 1000+log⁡21000 + \log 2. Shift by m=1000m = 1000: log⁡(e1000(e0+e0))=1000+log⁡2\log(e^{1000}(e^0 + e^0)) = 1000 + \log 2. Common mistakes are 10001000 (dropping the log⁡2\log 2) and log⁡2\log 2 (forgetting to add the shift back).

(b) nan. e1000e^{1000} overflows float64 (above about 709.8709.8) to ∞\infty, the sum is ∞\infty, and ∞/∞\infty / \infty is NaN. A common mistake is inf: the exponential does overflow to infinity, but the division then produces NaN. The true value, 0.5, is what the max-shifted softmax returns.

q9. The data are x=(108+1,108+2,108+3)x = (10^8 + 1, 10^8 + 2, 10^8 + 3). (a) Give their population variance 13∑i(xi−xˉ)2\frac{1}{3}\sum_i (x_i - \bar x)^2 exactly. [number] (b) In float64, which algorithm returns it to full precision: one pass (the mean of xi2x_i^2 minus the square of the mean) or two passes (compute the mean, then average the squared deviations)? Answer one or two. [choice]

Answer

(a) 2/32/3. The mean is 108+210^8 + 2, the deviations are −1,0,1-1, 0, 1, and 13(1+0+1)=2/3\frac{1}{3}(1 + 0 + 1) = 2/3. A common mistake is 1, the sample variance (dividing by n−1=2n - 1 = 2).

(b) two. Each xi2≈1016x_i^2 \approx 10^{16} exceeds 253≈9.0×10152^{53} \approx 9.0 \times 10^{15}, where the float64 gap is 2, so the squares are rounded by up to 1 each; the one-pass formula subtracts two numbers near 101610^{16} whose difference is 2/32/3, so the rounding errors are the same size as the answer (it can even come out negative). The two-pass method subtracts the mean first (exact here, Sterbenz), then squares small numbers.

q10. You sum the float32 values 1.01.0 followed by 10410^4 copies of 10−810^{-8}, left to right, rounding every addition to float32. (a) What does the naive loop return? [number] (b) Give the exact real sum 1+104⋅10−81 + 10^4 \cdot 10^{-8}, the value compensated (Kahan) summation recovers. [number]

Answer

(a) 11. The float32 gap at 1 is 2−23≈1.19×10−72^{-23} \approx 1.19 \times 10^{-7}, and 10−810^{-8} is below half of it (2−24≈5.96×10−82^{-24} \approx 5.96 \times 10^{-8}), so every 1+10−81 + 10^{-8} rounds back to 1. The running sum never moves, all 10410^4 times.

(b) 1+10−4=10001/100001 + 10^{-4} = 10001/10000. Kahan summation keeps the dropped low-order part in a compensation variable; once those parts accumulate past half a gap, they enter the sum. A common mistake is to swap the two answers: (a) asks what float32 returns, not the exact sum.

q11. Prove that softmax(x+c1)=softmax(x)\mathrm{softmax}(x + c\mathbf{1}) = \mathrm{softmax}(x) for every real cc, and that with c=−max⁡ixic = -\max_i x_i every exponent is at most 0, so no exi+ce^{x_i + c} overflows and the denominator is at least 1. [proof]

Answer

Claim. For x∈Rnx \in \mathbb{R}^n and every real cc, softmax(x+c1)=softmax(x)\mathrm{softmax}(x + c\mathbf{1}) = \mathrm{softmax}(x), where softmax(x)i=exi/∑jexj\mathrm{softmax}(x)_i = e^{x_i} / \sum_j e^{x_j}. With c=−mc = -m, m=max⁡jxjm = \max_j x_j, every exponent xi+cx_i + c is at most 0, so no term overflows, and the denominator is at least 1.

Shift invariance. Using ea+b=eaebe^{a + b} = e^a e^b, softmax(x+c1)i=exi+c∑jexj+c=ecexiec∑jexj=exi∑jexj=softmax(x)i,\mathrm{softmax}(x + c\mathbf{1})_i = \frac{e^{x_i + c}}{\sum_j e^{x_j + c}} = \frac{e^{c} e^{x_i}}{e^{c} \sum_j e^{x_j}} = \frac{e^{x_i}}{\sum_j e^{x_j}} = \mathrm{softmax}(x)_i, where cancelling ece^c is allowed because ec>0e^c > 0.

Bounds with c=−mc = -m. For every ii, xi≤mx_i \le m, so xi−m≤0x_i - m \le 0 and 0<exi−m≤10 < e^{x_i - m} \le 1. No term can overflow, whatever the size of xx. The index kk with xk=mx_k = m contributes e0=1e^0 = 1, and every other term is positive, so the denominator ∑jexj−m≥1\sum_j e^{x_j - m} \ge 1: it is never 0, and the division never produces ∞\infty or NaN. (The denominator is at most nn, also finite.)

What it avoids. Without the shift, exe^{x} overflows float32 for xx above about 88.788.7 and float64 above about 709.8709.8, and ∞/∞\infty / \infty is NaN: the failure of q8(b). With it, the same mathematical value is computed from numbers in (0,1](0, 1]. This is how softmax and logsumexp in Stable numerics are written.

A complete proof has four parts: the factor ece^c cancelled because it is positive, each shifted term in (0,1](0, 1], the denominator at least 1 because the maximum term is e0e^0, and the overflow thresholds this avoids.

The errors this set is designed to expose recur in real code. Each one maps to a problem:

PitfallSymptomHow to avoid it
Forgetting the exponent bias, or storing the implicit 1decompose_f32 disagrees with the bit viewq1: add 127 to the exponent; the leading 1 is implicit
Confusing the gap at 1 with the unit roundofftolerances off by a factor 2q2: the gap is 2−p2^{-p}, the unit roundoff 2−(p+1)2^{-(p+1)} is half of it
Thinking fp16 reaches 65536overflow to inf in mixed precisionq3: the top exponent field is reserved, so the maximum is (2−2−10)215=65504(2 - 2^{-10})2^{15} = 65504
Rounding ties up instead of to evenbf16 results differ from torch in the last bitq4: on a tie, pick the neighbour whose last fraction bit is 0
Trusting the naive formula’s outputa 0 where the answer is 5×10−55 \times 10^{-5}q6: rewrite with the conjugate so nothing nearly equal is subtracted
Exponentiating before subtracting the maxNaN from inf / inf at logit 1000q8 and q11: shift by the maximum first
One-pass variance on data far from 0negative or garbage variancesq9: subtract the mean before squaring
Accumulating a long sum naively in float32NLL totals that stop growingq10: compensated (Kahan) or pairwise summation

q1 to q4 become code in IEEE 754 anatomy, ulps, and round to nearest even (decompose_f32, ulp, round_to_bf16, round_to_fp16), and q8, q10, and q11 become logsumexp, softmax, kahan_sum, and pairwise_sum in Stable numerics. In AI Engineering, the fused cross-entropy of Losses with a fused backward computes log_softmax with the max shift, The Llama-family model: config, weight loading, and download loads bf16 weights (q2 to q4), and Mixed precision, loss scaling, gradient accumulation, and activation checkpointing explains why bf16 needs no loss scaling and fp16 does (q3). Problem set B makes q6’s loss of digits quantitative with error bounds and condition numbers.

  • Goldberg, “What Every Computer Scientist Should Know About Floating-Point Arithmetic” (1991).
  • Higham, Accuracy and Stability of Numerical Algorithms, chapters 1 to 4.
  • Implicit leading bit: the 1 in front of the fraction of a normal float, which is not stored.
  • Binade: an interval [2E,2E+1)[2^E, 2^{E+1}) within which floats are equally spaced.
  • Unit roundoff: the largest relative error of one rounding, half the gap at 1.
  • Cancellation: the loss of significant digits when subtracting nearly equal numbers that already carry rounding error.
  • Absorption: a small addend vanishing entirely because it is below half the gap of a large running sum.
  • A float is sign, biased exponent, and fraction; its value is (−1)s⋅1.f⋅2e−bias(-1)^s \cdot 1.f \cdot 2^{e - \text{bias}}, and the gap between neighbours doubles every binade (q1, q2).
  • bf16 keeps float32’s 8 exponent bits and drops 16 fraction bits, so it has float32’s range and a gap of 2−72^{-7} at 1; fp16 has a gap of 2−102^{-10} but overflows at 65504 (q2, q3).
  • Round to nearest, ties to even, sends an exact halfway value to the neighbour with an even last bit, which is what makes round_to_bf16 agree with torch bit for bit (q4).
  • Subtracting nearly equal numbers loses every digit they share; the fix is an algebraically equal formula that never subtracts them (q6, q7, q9).
  • Shifting softmax and log-sum-exp by the maximum changes nothing mathematically and removes overflow (q8, q11).