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,float16andbfloat16, including normal and subnormal values. - Construct overflow, underflow and cancellation failures and check an algebraically stable alternative.
Covers
Read
In this order, only the parts listed
0 of 3 done
- 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
dtypereference below before treating as the smallest positive representable value. Skip the Julia-specific Exercise 1.1.5. - 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.
- Inspect low-precision formatsPyTorch numerical type information
torch.finfo:bits,eps,max,tiny,smallest_normaland the note about subnormal numbers
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,float16andbfloat16have 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.tinythe 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.
Try it yourself
Closed-book, before checking anything
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.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
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.What is lost in at ?Answer
is about , where
float64spacing 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.Is the computed zero an underflow of the true answer?Answer
No. The true answer is about 5e-9, well within the normal
float64range. It is cancellation after an earlier rounding loss.4.Can and differ in
float64?AnswerYes. 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.
Practice
- FNC Python: enumerate a toy format and derive single precision
Exercises 1.1.1 (a) to (c) and 1.1.4 (a) to (b)
Attempt by hand before using
finfoornextafter. Translate the single-precision checks to NumPyfloat32rather than the exercise's Julia type name.
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'seps,maxandsmallest_normalfields and its subnormal note; inspectfloat64,float32,float16andbfloat16on 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. Inspectstate_dictfor optimizer restart; no network training is assigned. - SciPy minimize: BFGSSciPy · reference
Read the BFGS method description and
gtol,maxiter,c1andc2options. 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.