Skip to content

Numerical Methods and Floating Point

  • A float is a sign, an exponent, and a mantissa, and every operation rounds to the nearest representable value (round to nearest, ties to even). The gap between neighbours, one ULP, grows with magnitude.
  • Most numerical bugs are cancellation or overflow, and both have standard cures: subtract the maximum before exp (log-sum-exp), sum in pairs or with compensation (Kahan).
  • Tolerances are derived, not guessed. A dot product of length kk in a format with unit roundoff uu is off by at most about ku∑∣aibi∣k u \sum |a_i b_i|; tests should assert that bound, not 1e-5.
  • Low precision is an encoding problem. bf16, fp16, fp8, and MX formats differ in how many bits go to range vs precision; quantization picks a scale so values land where the format is dense.
  • Transcendentals in C are polynomials plus range reduction, and rsqrt is Newton’s method with a good first guess.

Read Goldberg once quickly, then Higham chapters 1 to 4 with a Python session: reproduce every cancellation example with numpy.float32. Decompose floats by hand (struct.pack) until reading bits is easy. In the course, M09.1 and M09.2 come in Pass 2 because autograd needs stable softmax; M09.3 to M09.6 come in Pass 6 with the inference engine and the C kernels.


Real numbers do not fit in 32 bits, so every computation is an approximation, and the job is to know how far off it is. Floating point fails in a few predictable ways (overflow, underflow, cancellation, accumulation of rounding error), each with a known algorithmic fix and a provable error bound. The same bound that explains why a kernel is accurate becomes the tolerance its test asserts.

Key ideas:

  • Layout: f32 is 1 sign, 8 exponent, 23 mantissa bits; bf16 keeps f32’s exponent with 7 mantissa bits; fp16 has 5 and 10.
  • Rounding to nearest even through a uint32 view is how bf16 is emulated in numpy (M09.1, used by L7.9 weight loading and L11.1 mixed precision).

Key ideas:

  • Log-sum-exp: log⁡∑iexi=m+log⁡∑iexi−m\log\sum_i e^{x_i} = m + \log\sum_i e^{x_i - m} with m=max⁡ixim = \max_i x_i; softmax and log-softmax follow, finite at ±104\pm 10^4, and a fully masked row gives zeros.
  • Summation: Kahan compensation and pairwise summation keep long reductions (perplexity over 10710^7 tokens) accurate (M09.2).

Key ideas:

  • Unit roundoff uu and γk=ku/(1−ku)\gamma_k = k u / (1 - k u) bound the error of kk accumulated operations.
  • Condition number κ(A)=∥A∥∥A−1∥\kappa(A) = \|A\| \|A^{-1}\| separates a bad problem from a bad algorithm.
  • Call sites: M09.3 gives the learner’s own differential tests their bounds, L8.5 the quantization error budget, and optional L9.1 the standalone C matmul parity bound.

Key ideas:

  • fp8 E4M3 and E5M2, and MX block formats with a shared E8M0 scale per 32 values (M09.4, used by L8.5, L9.4, and the KV format v2 migration).
  • Fast inverse square root: a bit-level first guess plus two Newton steps (M09.5, used by tl_rmsnorm_f32).
  • expf: range reduction x=kln⁡2+rx = k \ln 2 + r, a minimax polynomial for ere^r, and a scale by 2k2^k (M09.6, used by softmax, attention, and SiLU kernels).
ModuleTopicKindPass
S-M09aFloating point and stable numerics problem setsolve2
S-M09bError analysis and low-precision problem setsolve6
M09.1IEEE-754 anatomy, ULP, RNE rounding, bf16/fp16 emulationbuild2
M09.2Stable numerics: LSE, softmax, Kahan and pairwise sumsbuild2
M09.3Error analysis, condition numbers, tolerance budgetsbuild6
M09.4FP8 E4M3/E5M2, MXFP4/MXFP8 with E8M0 block scales; C conversionsbuild6
M09.5Fixed-point iteration, fast inverse sqrt in Cbuild6
M09.6Polynomial approximation and range reduction: expf in Cbuild6
M09.7Iterative solvers: conjugate gradientbuildoptional
#ModuleChapterKindPass
1M09.1IEEE 754 anatomy, ulp, round to nearest even, and bf16 and fp16 emulationbuild2
2M09.2Stable numerics: logsumexp, softmax, compensated sumsbuild2
3M09.3Error analysis, condition numbers, tolerance budgetsbuild6
4M09.4FP8 E4M3/E5M2, MXFP4/MXFP8 with E8M0 scalesbuild6
5M09.5Fast inverse square root in standalone C (optional)side6
6M09.6Polynomial approximation and range reduction: expf in C (optional)side6
7M09.7Low-precision conversions in standalone C (optional)side6
8S-M09aFloating point problem set, part a: representation, rounding, cancellationsolve2
9S-M09bFloating point problem set, part b: condition numbers, error bounds, Newton, polynomial approximation, low precisionsolve6
TrackConnection
Calculus 2Taylor series behind polynomial approximation
Matrix Calculus and AutodiffVJPs that need stable softmax and normalization
tinyllm Part 8quantization and its error budget
tinyllm Part 9the C kernels that call tl_expf and the fast rsqrt
LLM Systems: quantizationproduction quantization formats as depth