Mathematics · core

Numerical Stability

Floating point, catastrophic cancellation, and the log-sum-exp trick.

mathematicsnumerics

Mental model

Floating point stores a fixed number of significant digits, so error is relative, not absolute — and subtracting two nearly equal numbers throws away the leading digits that agreed, leaving noise promoted to the front. That is catastrophic cancellation, and it is why the naive variance formula and the quadratic formula both fail. The standard fixes are algebraic rather than numerical: reorder to avoid the subtraction, or shift into a domain where the range is safe, which is exactly what log-sum-exp does by subtracting the max before exponentiating.

How to study Numerical Stability

Begin by restating the mental model in your own words, then connect it to a concrete system you have built or operated. Name the mechanism, the constraint it addresses, and the trade-off it introduces. Use the linked roadmap and primary implementation references to check details, but close the source before writing your explanation. Retrieval is the learning step; rereading is only preparation.

Next, compare Numerical Stability with Softmax & Cross-Entropy, Multivariable Optimization. Ask what changes in correctness, latency, resource use, operability, and failure recovery. Complete Log-sum-exp without overflow and preserve the command, input, output, and one failed attempt as evidence. Finish by explaining the idea without jargon to someone who has not studied the track.

Proof of understanding

  • Explain the mechanism from first principles and identify the state it reads or changes.
  • Give one situation where the concept is the right choice and one where it is not.
  • Predict a realistic failure mode before running the drill, then compare the prediction with evidence.
  • Connect the result to a roadmap or build artifact instead of treating the concept as isolated trivia.

Where it matters

Every softmax and log-likelihood in a training loop, and financial code where a cent of drift compounds.

Common mistakes

  • Comparing floats with ==; use a tolerance scaled to the magnitude, not an absolute epsilon
  • Summing a long series left to right, letting small terms vanish against a large running total
  • Exponentiating raw logits and overflowing before the softmax normalises
  • Assuming double precision hides the problem — it postpones cancellation, it does not prevent it

Learn from primary sources

Use the linked roadmap context and practice prompt.

Practice and explain it back

Log-sum-exp without overflow

Implement logSumExp(xs) returning log(sum(exp(x))) that stays finite for large inputs by subtracting the maximum first. Must return a finite number for [1000, 1001, 1002], where the naive form overflows to Infinity.

Expected evidence: logSumExp([1000,1001,1002]) ~ 1002.4076; logSumExp([0,0]) ~ 0.6931

Open the interactive drill →

Review prompts

  • Subtracting two nearly equal floats is called catastrophic. What exactly is lost, and why does using doubles only postpone it?

Build evidence

Use a roadmap capstone to turn this concept into working evidence.

Prerequisites

Related concepts

Learning paths