Chapter 15: Numerical Methods and Floating Point
Real numbers do not fit in 32 bits, so every computation a model performs is an approximation, and the job of the engineer is to know how far off it is. Floating point fails in a few predictable ways: overflow, underflow, cancellation, and the accumulation of rounding error. Each failure has a known algorithmic fix and a provable error bound, and the same bound that explains why a kernel is accurate becomes the tolerance its test asserts. This chapter teaches you to see those failures coming, to fix them, and to prove that the fix works.
Concepts from other sections (23)
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.
- rounding error: the error from floating-point rounding, about in a difference quotient. Read more: 7.1 Derivatives and Finite Differences
- Log-sum-exp (LSE): , computed stably as . Read more: 15.2 Stable numerics: logsumexp, softmax, and compensated sums
- Condition number: how much the exact answer of a problem moves relative to a change in its input. Read more: 15.3 Error analysis, condition numbers, and tolerance budgets
- Tolerance budget: a test allowance that sums every deliberate perturbation and the rounding bound, and nothing else. Read more: 15.3 Error analysis, condition numbers, and tolerance budgets
- Microscaling (MX): a block format in which a fixed number of low-precision elements share one power-of-two scale. Read more: 15.4 FP8 and microscaling formats
- Newton’s method: the iteration , jumping to the zero of the tangent line. Read more: 7.2 Newton’s Method
- Range reduction: rewriting so that the polynomial only needs to be accurate on a small interval around 0. Read more: 15.6 Polynomial approximation and range reduction: expf in C
- E2M1 (FP4): a 4-bit float whose only magnitudes are . Read more: 15.4 FP8 and microscaling formats
- Round to nearest, ties to even: IEEE 754’s default rounding, which breaks exact ties toward the code whose last kept bit is 0. Read more: 15.1 IEEE 754 anatomy, ulps, and round to nearest even
- Dot product: ; it measures how much two vectors point the same way, and it is zero exactly when they are perpendicular. Read more: Chapter 10: Calculus 3
- Taylor series: the power series built from the derivatives of at ; truncating it gives the polynomials that libraries use to compute , , and . Read more: Chapter 8: Calculus 2
- stable softmax: softmax computed after subtracting the largest logit, so
expnever overflows. Read more: 25.6 Usage policy enforcement in AI Engineering - Vector-Jacobian product (VJP): , one step of reverse-mode differentiation. Read more: 10.3 Calculus 3 problem set: vectors, gradients, the chain rule, Hessians, multiple integrals, Lagrange
- Optimal finite-difference step: the step that balances truncation error against rounding error. Read more: 15.3 Error analysis, condition numbers, and tolerance budgets
- E4M3: an 8-bit float with 4 exponent bits (bias 7) and 3 fraction bits, no infinity, largest finite value 448. Read more: 15.4 FP8 and microscaling formats
- E5M2: an 8-bit float with 5 exponent bits (bias 15) and 2 fraction bits, IEEE-style infinities and NaNs, largest finite value 57344. Read more: 15.4 FP8 and microscaling formats
- E8M0: an 8-bit unsigned power-of-two scale with bias 127, used as the shared scale of an MX block. Read more: 15.4 FP8 and microscaling formats
- Cody and Waite split: representing as a short high part, whose products with are exact, plus a small low part. Read more: 15.6 Polynomial approximation and range reduction: expf in C
- Taylor polynomial: the degree- polynomial whose value and first derivatives at a point match those of . Read more: 8.1 Taylor series, remainder bounds, and range reduction
- Horner’s rule: evaluating a polynomial as nested multiply-adds from the innermost coefficient out. Read more: 15.6 Polynomial approximation and range reduction: expf in C
- Chebyshev nodes: the zeros of , which minimize the maximum of the interpolation node product on . Read more: 15.9 Problem set B: condition numbers, error bounds, Newton, polynomial approximation, and low precision
- VJP (vector-Jacobian product): , the gradient of ; what reverse mode computes. Read more: 10.2 Jacobians, the multivariable chain rule, and numeric JVP/VJP
- Error budget: the bad events an objective allows over its period, as a full outage. Read more: 1.1 Design review: the tracer and the serving platform in Systems Engineering
The chapter moves from representation to algorithms to low-level kernels. It opens by taking float32 apart bit by bit and rebuilding bfloat16 and float16 from it, because a float is just a sign, an exponent, and a mantissa, and every operation rounds to the nearest representable value. It then writes the stable reductions that every later chapter imports (log-sum-exp, softmax, and compensated sums), because most numerical bugs are cancellation or overflow, and both have standard cures. With the rounding model in hand, it derives rigorous error bounds and condition numbers and turns them into tolerance budgets, so that tests assert a derived number instead of 1e-5. The second half goes below 16 bits and into C: fp8 and the block-scaled microscaling formats, where low precision is an encoding problem; a fast inverse square root built from Newton’s method and a clever first guess; expf as range reduction plus a polynomial; and bit-exact low-precision conversions in standalone C. Two problem sets let you work every idea by hand.
Learning objectives
- Decode and construct IEEE 754 bit patterns, compute ulps, and round to bfloat16, float16, fp8, and FP4 with round to nearest, ties to even.
- Compute softmax, log-softmax, and log-sum-exp without overflow or underflow, and sum long sequences accurately with pairwise and Kahan summation.
- Derive rounding-error bounds for sums, dot products, and matrix products, and build tolerance budgets that include deliberate perturbations such as quantization.
- Distinguish an ill-conditioned problem from an unstable algorithm using condition numbers.
- Implement transcendental functions in C as range reduction plus a polynomial or a Newton iteration, with accuracy contracts stated in ulps.
Before you begin
Section titled “Before you begin”You need powers of two and logarithms from Precalculus, Newton’s method and finite differences from Calculus 1, Taylor series and their remainder bounds from Calculus 2, and the SVD from Linear Algebra. The stable softmax here is what the vector-Jacobian products of Matrix Calculus and Autodiff need in practice. A good way to read the chapter is with a Python session open: reproduce every cancellation example with numpy.float32, and decompose floats by hand with struct.pack until reading bits is easy.
The first two sections are needed early, as soon as you build autograd and need a stable softmax. The rest pairs naturally with the inference and kernel chapters of AI Engineering, Inference and Kernels in C, where quantization and the C kernels that call tl_expf and the fast rsqrt live. The C sections (5 to 7) are optional.
In this chapter
Section titled “In this chapter”- IEEE 754 anatomy, ulps, and round to nearest even. The float32 format bit by bit, the spacing of floats, and bfloat16 and float16 rounding as exact integer arithmetic on the bits, including the NaN case that the obvious trick gets wrong.
- Stable numerics: logsumexp, softmax, and compensated sums. The shift identity that keeps softmax and log-sum-exp finite at any scale, defined behavior for masked rows, and pairwise and Kahan summation with their error bounds.
- Error analysis, condition numbers, and tolerance budgets. The standard rounding model, bounds for dot products and matmuls, tolerance budgets that include quantization, condition numbers, and the optimal finite-difference step.
- FP8 and microscaling formats. E4M3, E5M2, FP4, and E8M0 block scales, with one generic rounding routine and the edge rules layered on top.
- Fixed-point iteration and fast inverse square root in C. Newton’s method as a fixed-point iteration, the bit-pattern first guess, and a third step in double that brings the result within 0.5004 ulp. Optional.
- Polynomial approximation and range reduction: expf in C. Range reduction with the Cody and Waite split, a degree-7 Taylor polynomial by Horner’s rule, and exact scaling at the overflow and underflow edges. Optional.
- Low-precision conversions in C. One integer rounding routine shared by E4M3, E5M2, bfloat16, and binary16, bit-exact with the Python version. Optional.
- Problem set A: representation, rounding, and cancellation. Pen-and-paper problems on float fields, gaps, ties, cancellation, stable rewrites, and the max shift, with worked answers.
- Problem set B: condition numbers, error bounds, Newton, polynomial approximation, and low precision. Pen-and-paper problems on conditioning, , convergence rates, polynomial degree and Chebyshev nodes, and fp8 and MX rounding, with worked answers.
Related chapters
Section titled “Related chapters”Taylor series in Calculus 2 are behind every polynomial approximation here. Matrix Calculus and Autodiff uses the stable softmax and normalization in its VJPs. In AI Engineering, Inference applies the quantization formats and their error budget, and Kernels in C contains the C kernels that call tl_expf and the fast rsqrt. For production quantization formats in more depth, see Quantization: Math to Code in AI Engineering.
Further reading
Section titled “Further reading”- Goldberg, What Every Computer Scientist Should Know About Floating-Point Arithmetic (free). Read it once quickly before starting.
- Higham, Accuracy and Stability of Numerical Algorithms (SIAM, 2nd ed.). Chapters 1 to 4 pair with the first three sections.
- Trefethen and Bau, Numerical Linear Algebra (SIAM).
- Muller et al., Handbook of Floating-Point Arithmetic.
- The OCP Microscaling Formats (MX) v1.0 spec (free).
- Micikevicius et al., FP8 Formats for Deep Learning (free).