Every numerical answer is wrong by some amount. We learn to measure that amount, predict how it grows, and avoid the one operation that destroys accuracy: subtracting nearly equal numbers.
Md Mahbub E Noor · Assistant Professor CSE-3107 Numerical Methods · B.Sc. 3rd Year 1st Semester Department of CSE, University of Barishal
Today's goals
Compute absolute, relative and percentage errors and count correct significant digits
Distinguish round-off error from truncation error, with examples of each
Use the error propagation rules for +,−,×,÷ and the general formula
Recognise catastrophic cancellation and rewrite formulas to avoid it
Find the best step size when truncation and round-off errors compete
Last time
Round-off: relative error up to 10−16 per number (L2). Truncation: the first neglected Taylor term, O(hp) (L3).
Today
How do those tiny errors, plus the errors of stopping a method early, combine and grow through a calculation?
Every numerical answer has two kinds of error.Truncation: we stopped a process early. A smaller step helps.Round-off: the computer keeps limited digits. A smaller step hurts.What we actually get is the red V: the bigger of the two.So "smaller h" is not always better: there is a best step size.
In plain words
Every computed answer is a little wrong: the question is how much
Two causes: stopping early (truncation) and limited digits (round-off)
Making one smaller can make the other bigger
New words today
absolute error
|true − approximate|
relative error
absolute error ÷ |true value|
significant digits
digits that carry real information
cancellation
losing digits when subtracting nearly equal numbers
Md Mahbub E Noor
Lecture 4: Errors and Their Propagation
University of Barishal
3 / 17
Measuring error
True value x, approximation x∗.
Absolute errorEa=∣x−x∗∣
Relative errorEr=∣x∣∣x−x∗∣
Percentage errorEp=100×Er%
Relative error is scale-free: an error of 1 cm in 1 km (10−5) is far better than 1 cm in 10 cm (10−1). Numerical analysts almost always report relative error.
Approximation of π
Ea
Er
3.14
0.0015927
5.07×10−4
22/7 = 3.142857
0.0012645
4.02×10−4
3.1416
0.0000073
2.34×10−6
Md Mahbub E Noor
Lecture 4: Errors and Their Propagation
University of Barishal
4 / 17
How many significant digits are correct?
Rulex∗ is correct to n significant digits if Er≤21×101−n
Decimal placesx∗ is correct to d decimal places if Ea≤21×10−d
3.14: Er=5.07×10−4≤0.5×10−2 ✓ but >0.5×10−3 ✗: 3 significant digits
3.1416: Ea=7.3×10−6≤0.5×10−4: correct to 4 decimal places
Double precision: Er≤1.1×10−16 gives about 16
Iterations: stop when ∣xk+1−xk∣<21×10−d for d decimal places (Lecture 1)
Md Mahbub E Noor
Lecture 4: Errors and Their Propagation
University of Barishal
5 / 17
Two sources of error in every computation
Lecture 2
Round-off error
From storing numbers with finitely many digits
Example: 32→0.6667, 0.1→0.1000000000000000055…
Grows slowly with the number of operations
Reduced by higher precision (double, not float)
+
this course
Truncation error
From replacing an infinite or exact process by a finite one
Stopping an iteration, a finite step h, a cut series
Reduced by more terms, smaller h, more iterations
Total error = round-off + truncation. Making h smaller cuts truncation error but adds round-off error: a balance we measure later in this lecture and meet again in Lecture 19 (numerical differentiation).
Md Mahbub E Noor
Lecture 4: Errors and Their Propagation
University of Barishal
6 / 17
How errors propagate through arithmetic
Inputs x±Δx and y±Δy. Worst-case (maximum) error of the result:
Sum and difference: absolute errors addΔ(x±y)≤Δx+Δy
Product and quotient: relative errors add∣xy∣Δ(xy)≈∣x∣Δx+∣y∣Δy,∣x/y∣Δ(x/y)≈∣x∣Δx+∣y∣Δy
General functionΔf≈∂x∂fΔx+∂y∂fΔy+⋯
Powers: ∣xn∣Δ(xn)≈∣n∣∣x∣Δx
The general formula comes from the first-order Taylor expansion (Lecture 3)
Danger: in x−y the absolute error stays, but if x−y is tiny the relative error explodes
Md Mahbub E Noor
Lecture 4: Errors and Their Propagation
University of Barishal
7 / 17
Problem 1: relative error of a formula
Problem 1If u=z44x2y3 and the errors in x,y,z are each 0.001, find the maximum relative error in u at x=y=z=1.
1
Take logarithms: lnu=ln4+2lnx+3lny−4lnz
2
Differentiate: uΔu=2xΔx+3yΔy−4zΔz
3
Worst case: all terms add: uΔumax=210.001+310.001+410.001
4
=(2+3+4)×0.001=0.009
Maximum relative error =0.009 (0.9%). The powers multiply the relative errors: z4 contributes the most.
Second example
Rectangle area
l=10.0±0.05 cm, w=5.0±0.05 cm. A=50 cm²; ΔA≈wΔl+lΔw=0.25+0.5=0.75 cm². Relative: 0.5%+1%=1.5%.
Md Mahbub E Noor
Lecture 4: Errors and Their Propagation
University of Barishal
8 / 17
Catastrophic cancellation
Subtracting two nearly equal numbers cancels their leading digits; only the noisy trailing digits survive.
4-digit arithmetic: 1001−1000=31.64−31.62=0.02
True value 0.0158074: relative error 26.5%, from inputs accurate to 0.02%!
Rewrite (multiply by the conjugate): 1001+10001=63.261=0.01581 (error 0.01%)
The subtraction itself is exact; it exposes the earlier rounding errors
Digit view
31.6385… − 31.6228… = 00.0157…
In 4 digits: 31.64 − 31.62: the two shared digits "31.6" cancel, leaving one digit of noise.
Md Mahbub E Noor
Lecture 4: Errors and Their Propagation
University of Barishal
9 / 17
Explore: naive vs rewritten formulas
x21−cosx should be ≈ 0.5 for small x
Move x towards 10−8: the naive formula returns 0 (100% error)
Rewritten x22sin2(x/2) stays accurate
Try the other two expressions in the menu
Md Mahbub E Noor
Lecture 4: Errors and Their Propagation
University of Barishal
10 / 17
Fixing the quadratic formula
Problem 2Solve x2+108x+1=0. The roots are about −108 and −10−8.
1
Naive small root: x1=2−108+1016−4: two numbers that agree in about 16 digits are subtracted
Use x1x2=c/a=1: compute the large root safely, x2=2−108−1016−4 (no cancellation)
4
Then x1=ax2c=−b−b2−4ac2c=−1.0000×10−8 ✓
Rule: compute the root where −b and ±b2−4ac have the same sign, then the other from x1x2=c/a.
Md Mahbub E Noor
Lecture 4: Errors and Their Propagation
University of Barishal
11 / 17
Truncation and round-off together: e−20 by Taylor series
ex=∑xn/n! converges for every x (no truncation problem with enough terms)
For x=−20 the terms reach 4.3×107 with alternating signs: huge cancellation
The sum comes out wrong in every digit, even negative!
Cure: compute 1/e20 (all terms positive)
Md Mahbub E Noor
Lecture 4: Errors and Their Propagation
University of Barishal
12 / 17
Truncation meets round-off: the best step size
f′(x)≈hf(x+h)−f(x) (Lecture 3): truncation error ∝ h (right side of the V)
But f(x+h)−f(x)cancels: round-off error ≈ ε/h grows as h shrinks (left side)
Best forward h≈ε≈10−8; best central h≈ε1/3≈10−5
Smallest possible error: about 10−8 (forward), 10−11 (central)
Md Mahbub E Noor
Lecture 4: Errors and Their Propagation
University of Barishal
13 / 17
Beyond the classroom
Where this shows up in CSE and machine learning
📊
Variance in one pass
σ2=x2−xˉ2 cancels badly when the mean is large: data near 108 give 22.0 instead of 22.5, and near 109 the result is pure noise (it can even be negative). Libraries subtract the mean first (two passes) or use Welford's method.
🧠
Log-sum-exp and softmax
log∑ezi is computed as m+log∑ezi−m with m=maxzi: no overflow, no loss of digits.
🔊
Audio in decibels
Speech energy in dB uses 10log10(E+ϵ); the small ϵ stops log0 on silent frames.
💰
Money
Banks never store taka as binary floats: 0.1 is inexact. They use integers (paisa) or decimal arithmetic.
Md Mahbub E Noor
Lecture 4: Errors and Their Propagation
University of Barishal
14 / 17
Practice
Practice problem
Problem 3(a) Define absolute, relative and percentage errors. (b) If π is approximated by 3.14, find the three errors. (c) Explain, with an example, why the subtraction of two nearly equal numbers causes loss of significant digits and how it can be avoided.
How to write a complete answer
Write each definition as a formula with a one-line meaning
Show substitution: Ea=∣3.1415927−3.14∣=0.0015927, Er=0.0015927/3.1415927=5.07×10−4, Ep=0.0507%
Cancellation: use the 4-digit 1001−1000 example; give both the wrong and the rewritten result
State the cure in general: rewrite with conjugates, trig identities, or series for small arguments
Md Mahbub E Noor
Lecture 4: Errors and Their Propagation
University of Barishal
15 / 17
Checkpoint
Checkpoint · think, then discuss with your neighbourWhich expression is computed accurately in double precision for x=10−10?
A1+x−1
B1+x+1x
C(1+x)−1
Dxex−1
B. It equals A exactly (multiply by the conjugate) but has no subtraction of nearly equal numbers. A, C and D all subtract 1 from a number very close to 1.
Md Mahbub E Noor
Lecture 4: Errors and Their Propagation
University of Barishal
16 / 17
Summary
Key points
Ea=∣x−x∗∣, Er=Ea/∣x∣, Ep=100Er; correct to n significant digits if Er≤21101−n
Round-off: finite digits. Truncation: finite process. Total error is both
Absolute errors add in ±; relative errors add in ×,÷; powers multiply them
Subtracting nearly equal numbers destroys relative accuracy: rewrite the formula
Total error = truncation + round-off: a V shape in h; best h≈ε (forward) or ε1/3 (central)
Homework
Find Ea, Er and Ep when 2 is approximated by 1.414. How many significant digits are correct?
If V=πr2h with r=2.0±0.01 and h=5.0±0.02, find the maximum relative error in V.
Rewrite ln(x+1)−lnx for large x to avoid cancellation. Test both forms in Python for x=1012.
Lab: run the variance program and explain why the one-pass formula fails.
Next lecture
Conditioning and stability. Why some problems amplify every small error (and no algorithm can help), while some algorithms ruin perfectly good problems.
Md Mahbub E Noor
Lecture 4: Errors and Their Propagation
University of Barishal
17 / 17
Keys →Space next step · ← back A reveal the whole slide number + Enter jump to slide S slide list · F full screen · N notes [] previous / next lecture · L lab In a code box: Ctrl+Enter runs the code