Skip to content
BIASED

Conditioning and numerical stability

Block 1 of 7~2 min

Floating-point numbers

The topic Duality ended with certificates: a primal point, a dual point and a gap that bounds how far both are from the optimum. Every number in such a certificate, like every gradient, solve and decomposition in this course, is computed in floating point. This topic asks how many digits of an answer survive.

A floating-point number stores a fixed number of significant bits and an exponent, like scientific notation in base 2. The gap between 11 and the next representable number is the machine epsilon εmach\varepsilon_{\text{mach}}. Each arithmetic operation returns the exact result rounded to the nearest representable number:

fl⁡(a∘b)=(a∘b)(1+δ),∣δ∣≤12 εmach,∘∈{+,−,×,/}.\operatorname{fl}(a \circ b) = (a \circ b)(1 + \delta), \qquad \lvert \delta \rvert \le \tfrac12\,\varepsilon_{\text{mach}}, \qquad \circ \in \{+, -, \times, /\}.

So a single operation is accurate to a relative error of about εmach\varepsilon_{\text{mach}}. The formats used in ML differ a lot:

Format Significant bits εmach\varepsilon_{\text{mach}} Largest number
float64 5353 2−52≈2.2⋅10−162^{-52} \approx 2.2 \cdot 10^{-16} 1.8⋅103081.8 \cdot 10^{308}
float32 2424 2−23≈1.2⋅10−72^{-23} \approx 1.2 \cdot 10^{-7} 3.4⋅10383.4 \cdot 10^{38}
bfloat16 88 2−7≈7.8⋅10−32^{-7} \approx 7.8 \cdot 10^{-3} 3.4⋅10383.4 \cdot 10^{38}
float16 1111 2−10≈9.8⋅10−42^{-10} \approx 9.8 \cdot 10^{-4} 6550465504

A relative error of 10−1610^{-16} leaves the first 1616 decimal digits intact, so float64 carries about 1616 significant digits.

Two consequences show up everywhere. First, adding to aa a positive number smaller than half the gap between aa and the next representable number changes nothing. Right after 11 that gap is εmach\varepsilon_{\text{mach}}, so in float64 1 + 1e-16 == 1 is True. Second, the range is finite. exp(710) is already inf in float64, and exp(89) is inf in float32.

Answers are saved on the server and tied to this browser until you sign in.

Go deeper — optional

Code and labs do not count toward the main percentage or the certificate.

  • Log-sum-exp in codeoptional

    Implement a numerically stable log-sum-exp — the backbone of stable softmax.

    ~15 min · easier on a computer · The Conditioning and numerical stability topic, log-sum-exp block.

    Open practice