Floating-point stability
Quick answer
Numerical stability means computing a result without unnecessarily magnifying rounding or exceeding the representable range. Floating-point numbers approximate real numbers using finite precision. Algebraically equivalent expressions can behave differently on a computer. Stable softmax and log-sum-exp are useful examples when training classifiers.
Rounding, overflow and underflow
Most Python floats on this platform are binary64. Decimal fractions such as 0.1 usually have no exact binary representation. Adding a tiny value to a much larger one can round away the tiny contribution; subtracting nearly equal approximations can lose significant digits. Overflow occurs when a result is too large to represent, whereas underflow occurs when a tiny nonzero magnitude falls into the subnormal range (and may round to zero). Python math.exp raises OverflowError for a large positive argument on this environment; different numerical libraries may return infinity or issue a warning. Check finite values with math.isfinite and use math.isclose with tolerances chosen for the task.
import mathprint(0.1 + 0.2 == 0.3)print(math.isclose(0.1 + 0.2, 0.3))print((1e16 + 1.0) - 1e16)print(math.exp(-1000))try: math.exp(1000)except OverflowError: print("overflow")Expected output (local Python 3.13.5):
FalseTrue0.00.0overflowThe small increment is lost; the negative exponential rounds to zero and the positive exponential exceeds the supported range.
Shift logits before exponentiating
Softmax turns a nonempty vector of finite scores (logits) into nonnegative probabilities that sum to one up to rounding. Directly evaluating exp(z_i)/sum(exp(z_j)) can overflow. Subtract the largest score m: exp(z_i-m)/sum(exp(z_j-m)). The common factor cancels, and at least one exponential equals one. For finite moderate scores this prevents exponential overflow and an all-zero denominator. Extremely small probabilities may still underflow. Similarly log-sum-exp (LSE) = m + log(sum(exp(z_i-m))). Compute log probabilities as (z_i-m) - log(sum(exp(z_j-m))) rather than log of a rounded probability. This lesson uses finite bounded inputs; empty or nonfinite scores need an explicit policy. Production tensor libraries provide stable implementations.
import mathz = [1000, 1001, 1002]m = max(z)e = [math.exp(x-m) for x in z]s = sum(e)print([round(x/s, 6) for x in e])print(round(m+math.log(s), 6))print(round(sum(x/s for x in e), 6))Expected output (local Python 3.13.5):
[0.090031, 0.244728, 0.665241]1002.4076061.0Subtracting the same constant keeps relative probabilities unchanged. The shifted exponentials never exceed one.
Common mistakes
Do not compare computed real-valued quantities by exact equality when rounding is expected. Do not blindly add an epsilon to every denominator: this changes the formula and may hide invalid input. Do not assume max shifting fixes every numerical issue; extreme differences can still underflow, and invalid logits must be handled. Avoid rounding every training step.
Practice and next steps
Compute softmax for [0,0] and [1000,1000]; explain why they match. For [0,-1000], explain why the smaller computed probability may be zero even though its exact mathematical value is positive. Next, diagnose optimization traces.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Floating-point stability — knowledge check
Apply optimization reasoning to synthetic examples.
Why does 0.1+0.2 differ from 0.3 in ordinary Python float arithmetic?
What happens when exp(-1000) rounds to zero?
What is the max-shift for [1000,1002]?
What is softmax of equal finite scores [1000,1000]?
Why calculate log probabilities directly from shifted logits?