Unit 6 of 8 · about 4 hours · derive, measure

Floating point, rounding, overflow and cancellation

Mathematical real arithmetic is not a machine's arithmetic. A dtype's range and spacing predict different failures, and rewriting a formula can matter more than increasing precision.

You'll be able to

  • Derive spacing and unit roundoff from a binary significand and distinguish precision from exponent range.
  • Compare float64, float32, float16 and bfloat16, including normal and subnormal values.
  • Construct overflow, underflow and cancellation failures and check an algebraically stable alternative.

Covers

Floating-point representation, rounding and machine precisionOverflow, underflow and catastrophic cancellation
1

Read

In this order, only the parts listed

0 of 3 done

  1. Binary representation and roundingFundamentals of Numerical Computation

    Section 1.1: Definitions 1.1.1 to 1.1.2, equations 1.1.1 to 1.1.6, and Sections 1.1.2 to 1.1.3

    Use the Python examples. The main-text minimum-range discussion concerns normal values; read footnote 2 and the dtype reference below before treating as the smallest positive representable value. Skip the Julia-specific Exercise 1.1.5.

  2. Conditioning and unstable cancellationFundamentals of Numerical Computation

    Sections 1.2.1 to 1.2.2, condition numbers and error estimates, on the preceding Problems and conditioning page; Section 1.4.1 and Examples 1.4.1 to 1.4.2 on Stability

    Focus on subtractive cancellation and the stable quadratic-root rewrite; skip the later polynomial-root perturbation demo.

  3. Inspect low-precision formatsPyTorch numerical type information

    torch.finfo: bits, eps, max, tiny, smallest_normal and the note about subnormal numbers

2

Focus on

  • With total significand bits including the hidden leading one, spacing in is . is the gap above 1; unit roundoff for round-to-nearest is . The relative bound needs normal-range results and no overflow.
  • float64, float32, float16 and bfloat16 have and exponent fields of 11,8,5,8 bits respectively. bfloat16 has wide range but coarser spacing than float16.
  • Overflow produces infinities; gradual underflow first reaches subnormals with reduced relative precision. is the smallest positive normal, not the smallest positive representable value.
  • Subtracting nearby rounded operands can destroy relative accuracy even when the subtraction itself is exact. Separate conditioning of the requested difference from avoidable instability in how its operands were formed.
Common mistakes · 3
  • Calling the smallest representable number, or calling finfo.tiny the smallest subnormal.
  • Assuming bfloat16 is more precise because its exponent range is larger.
  • Applying a relative error tolerance at a true zero, or claiming that a stable rewrite recovers information already rounded out of its inputs.
3

Try it yourself

Closed-book, before checking anything

By hand enumerate the fractional-bit system in FNC Exercise 1.1.1 and derive its spacing. Then use np.finfo for np.float64, np.float32 and np.float16, and torch.finfo(torch.bfloat16) on CPU for bfloat16. Print eps, tiny and max to three significant digits: float64 (2.22e-16,2.23e-308,1.80e308), float32 (1.19e-7,1.18e-38,3.40e38), float16 (9.77e-4,6.10e-5,6.55e4), bfloat16 (7.81e-3,1.18e-38,3.39e38). Derive and check each within relative scalar tolerance 1e-6. Inspect np.nextafter(dtype(0),dtype(1)), and torch.nextafter on CPU bfloat16 scalars, to see values below . In NumPy float64 set x=np.float64(1e8): returns 0, while returns about 5.00e-9. Rationalize the expression by hand and bound the exact result's difference from by ; allow absolute 1e-20 for the stable scalar. Finally evaluate np.exp(np.float64(1000)) and np.exp(np.float64(-1000)) inside np.errstate(over='ignore',under='ignore'): outputs are inf and 0. These are range failures, not exact evaluations of . No random draws are needed.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Why is round-to-nearest error bounded by eps/2 in the normal range?Answer

    In each binade the absolute error is at most half its spacing. Dividing by bounds relative error by . The lower bound on uses normal binade representation; subnormals instead have fixed absolute spacing. Overflow is excluded.

  2. 2.What is lost in at ?Answer

    is about , where float64 spacing is 2, so adding 1 rounds away under ties-to-even. The square root returns and subtraction gives zero. Rationalization avoids subtracting nearly equal operands; it does not make exact.

  3. 3.Is the computed zero an underflow of the true answer?Answer

    No. The true answer is about 5e-9, well within the normal float64 range. It is cancellation after an earlier rounding loss.

  4. 4.Can and differ in float64?Answer

    Yes. Under round-to-nearest ties-to-even, the first evaluates to zero and the second to about 1.11e-16. The real-arithmetic associative law does not transfer to rounded operations.

5

Practice

You're done when

You can derive each format's spacing, distinguish from normal and subnormal limits, and explain why the stable cancellation rewrite passes its absolute-error check while the naive formula fails.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 9 more in the section
  • Fundamentals of Numerical ComputationDriscoll & Braun · required

    Study §§1.1-1.4, 2.7-2.8, 3.2-3.4 and 4.2-4.3 for floating point, conditioning, stability, least squares and iteration error, in the Python edition.

  • PyTorch numerical type informationPyTorch · reference

    Read torch.finfo's eps, max and smallest_normal fields and its subnormal note; inspect float64, float32, float16 and bfloat16 on CPU.

Also in this section

  • Stanford EE364a - Convex OptimizationStanford Engineering Everywhere · primary

    Follow lectures 1-9 and 15-16; use lecture 17 only for KKT systems. Leave full interior-point treatment for later.

  • Convex OptimizationBoyd & Vandenberghe · required

    Read Chapters 1-5 and 9 with the course; use Chapter 10 only for equality-constrained Newton steps.

  • D2L - Optimization AlgorithmsD2L authors · required

    Use the convexity, GD, SGD, minibatch, momentum, AdaGrad, RMSProp, Adam and scheduler sections for ML derivations and code.

  • CVXPY basic and DCP tutorialsCVXPY documentation · support

    Use as the solver oracle after deriving a constrained problem and its KKT conditions by hand.

  • Proximal AlgorithmsNeal Parikh and Stephen Boyd · reference

    Read Sections 1.1, 4.2 and the elementwise soft-thresholding formula in 6.5.2, then the lasso formulation on p. 196. Skip 7.1.1's penalty-scaling and Gram-rewrite errors on p. 197; derive the update from 4.2 and 6.5.2.

  • PyTorch optimizer and gradient-clipping contractsPyTorch · reference

    Use AdamW's algorithm and clip_grad_norm_'s parameters to locate weight decay and clipping precisely. Inspect state_dict for optimizer restart; no network training is assigned.

  • SciPy minimize: BFGSSciPy · reference

    Read the BFGS method description and gtol, maxiter, c1 and c2 options. Supply an analytic gradient and compare residuals and evaluation counts, not just iterations.

  • The Accuracy of Floating Point SummationNicholas J. Higham · reference

    Read Section 2's recursive-sum error bound and Section 3's pairwise and compensated summation algorithms. Leave the statistical error models and historical machine details aside.

  • Accurately computing running varianceJohn D. Cook · support

    Read the Welford mean and centered-sum recurrences. Prove the centered-sum invariant and translate the recurrence to NumPy; skip the C++ class.