Lesson 1 of 8
Floating point: a finite number line
Computers store a limited set of numbers, so most real values are rounded to a nearby representable value.
Core lesson · about 7 minutes · 3 guided parts
Imagine a ruler with movable spacing: many fine marks near zero and wider gaps far away. A computer number line works similarly, so arithmetic lands on nearby marks rather than every possible real number.
Your learning route
Build the idea one piece at a time
- How numbers are represented
- Roundoff, cancellation, and summation
- Why a computer can lose a small but real difference
How numbers are represented
A floating-point number stores a sign, a significand, and an exponent, like scientific notation with a fixed number of digits.
In a binary format, x≈(−1)ˢ×1.f×2ᵉ for normal numbers. The exponent controls scale and the fraction controls precision.
Machine epsilon describes the gap from 1 to the next representable number. Relative gaps are roughly stable for normal values, while absolute gaps grow with magnitude.
Overflow produces values too large for the format; underflow reaches tiny subnormal values or zero. Neither is the same as ordinary rounding.
A decimal such as 0.1 repeats in base two for the same reason one third repeats in base ten: the denominator has prime factors outside the base.
An ulp is the gap between neighbouring representable numbers near a value, making it a local measure of absolute precision that grows with the exponent.
Rounding mode decides how an exact result between representable numbers is chosen; round-to-nearest is common, but directed modes are useful for rigorous bounds.
fl(x)=x(1+δ), usually |δ|≤uWorked example
In a toy system with three significant decimal digits, store 12.345 and then multiply the stored value by 10.
- Round 12.345 to three significant digits: 12.3.
- The representation has already lost 0.045.
- Multiply the stored value: 12.3×10=123.
- The exact unrounded product would have been 123.45, showing propagated representation error.
Answer: Stored value 12.3; computed product 123
Roundoff, cancellation, and summation
Subtracting two nearly equal rounded numbers can erase their shared leading digits and expose noisy leftovers.
Catastrophic cancellation is loss of relative accuracy after subtracting close quantities. Rewrite formulas to avoid it when possible.
Floating-point addition is not associative: (a+b)+c may differ from a+(b+c) because each intermediate result rounds.
Summing from small magnitude to large, pairwise summation, or Kahan compensated summation can reduce lost low-order contributions.
Cancellation does not create absolute error from nothing; it turns earlier small absolute rounding errors into a large relative error when the true difference is tiny.
Summation itself can be ill-conditioned when large positive and negative terms nearly cancel, so even a stable summing method cannot promise small relative error to a near-zero total.
A stable reformulation should be algebraically checked and tested across ordinary, tiny, huge, and sign-changing inputs because one branch may not be safe everywhere.
|x̂−x|/|x| = relative error, when x≠0Worked example
Why is √(x²+1)−x unstable for large positive x, and give a stable equivalent.
- Both √(x²+1) and x are large and almost equal.
- Their subtraction cancels leading digits.
- Multiply numerator and denominator by the conjugate √(x²+1)+x.
- The stable form is 1/[√(x²+1)+x].
Answer: Use 1/(√(x²+1)+x)
Practical deep dive for this lesson
Why a computer can lose a small but real difference
Floating-point numbers store a fixed number of significant binary digits across a huge range. Rounding is usually tiny, but subtraction or scale imbalance can expose and magnify what was discarded.
A computer does not store every real number. It stores sign, exponent, and a finite significand, much like scientific notation with a fixed number of digits.
Values such as decimal 0.1 usually have repeating binary expansions, so the stored value is the nearest representable neighbour rather than exact 0.1.
Machine epsilon describes spacing near one, but absolute spacing grows with magnitude. Near 10¹⁶ in binary64, neighbouring representable numbers are farther apart than one.
Adding a tiny number to a huge one may therefore leave the stored huge number unchanged. The small contribution falls below available resolution.
Catastrophic cancellation happens when two close rounded numbers are subtracted. Leading digits cancel and the remaining result consists largely of earlier rounding error.
Algebraic rewriting can avoid this. For small x, √(1+x)−1 is better computed as x/[√(1+x)+1], which replaces dangerous subtraction with stable operations.
Overflow means a magnitude exceeds the format's range; underflow makes tiny values round toward zero or subnormal values. Both can break later formulas.
Stable log-sum-exp, compensated summation, wider accumulators, and careful scaling are algorithmic responses to these representation limits.
Tests should compare within tolerances tied to the calculation's scale and real requirement, not demand exact equality from rounded approximations.
Numerical literacy separates harmless last-bit differences from errors that reverse a decision, violate conservation, or accumulate into a material total.
√(1+x)−1=x/[√(1+x)+1]Worked example
Explain why evaluating √(1+x)−1 directly is risky for tiny x, and compute a stable form for x=10⁻⁸.
- The exact expression subtracts two numbers near one: √(1+10⁻⁸) and 1.
- Those leading digits are almost identical, so their subtraction can erase significant digits in finite precision.
- Multiply numerator and denominator by the conjugate √(1+x)+1.
- Use (√(1+x)−1)(√(1+x)+1)=x.
- Therefore √(1+x)−1=x/[√(1+x)+1].
- For x=10⁻⁸, the denominator is about 2.000000005.
- The quotient is about 4.9999999875×10⁻⁹, obtained without subtracting nearly equal stored values.
- A high-precision reference or language function designed for this expression can verify the retained digits.
Answer: Use x/[√(1+x)+1]≈4.9999999875×10⁻⁹.
Check your picture
Why can 0.1+0.2 differ slightly from 0.3 in binary floating point?
Reveal answer
Most decimal fractions, including 0.1 and 0.2, have no finite binary representation and must be rounded.
This lesson versus the whole chapter
Your topic-specific practical example is above.
The final lesson collects five longer chapter-wide workflows for machine learning, LLMs, trading, physics, and everyday decisions.
Formula shelf
Symbols with meaning
fl(x)=x(1+δ), |δ|≤uA rounded value differs by a small relative error under normal conditions.
|x̂−x|/|x|, x≠0Error measured relative to a nonzero true scale; use absolute or application-specific scaling when x=0.
κ(A)=||A||||A⁻¹||Worst-case amplification of relative perturbations.
f′(x)≈[f(x+h)−f(x−h)]/(2h)Second-order numerical derivative estimate.
∫f≈(h/3)[f₀+4f₁+2f₂+⋯+fₙ]Quadratic-piece numerical integration.
Xₖ=Σₙ₌₀ᴺ⁻¹xₙe−2πikn/NRe-express samples as frequency components for filtering, convolution, and spectral differentiation.
R=[2ᵖA(h/2)−A(h)]/(2ᵖ−1)Cancel a leading Chᵖ approximation error.
LSE(z)=m+log Σᵢeᶻⁱ⁻ᵐEvaluate a log of exponential sums without overflow, where m=maxᵢzᵢ.
xₙ=N⁻¹Σₖ₌₀ᴺ⁻¹Xₖe2πikn/NReconstruct samples from their complex Fourier coefficients under this normalisation.
fN=fₛ/2Upper boundary for unique frequencies in ideal uniform sampling; practical content stays below it.
X′ⱼ=i(2πj/L)XⱼDifferentiate a periodic Fourier mode using its signed physical wavenumber.
r=b−Ax̂How well a computed vector satisfies the equations.
xₖ₊₁=xₖ−f(xₖ)/f′(xₖ)Use the tangent line to improve a root estimate.
Before moving on
You are ready when you can…
- Explain rounding, cancellation, overflow, and relative error.
- Separate conditioning of a problem from stability of an algorithm.
- Choose sensible steps for numerical derivatives and quadrature.
- Use Richardson extrapolation only when the observed error follows its assumed order, and evaluate log-sum-exp and related kernels stably.
- Construct and invert a DFT, explain FFT speed and zero-padded convolution, and convert bins to physical frequencies.
- Apply Nyquist's limit and perform correctly scaled spectral differentiation on smooth periodic data.
- Solve linear systems with factorizations or appropriate iterations.
- Use safeguarded root finding and defend a stopping tolerance.