TExcelTAD
Home › Calculus Essentials › Approximation Error

Approximation Error

Every number a program computes from a series, a step size or a sum is an approximation. This lesson shows how to bound the error of a Taylor polynomial, how many terms to keep, and why making a step smaller can make a result worse once floating-point rounding joins in.

Two kinds of error

Error Comes from Shrinks when Example
Truncation error cutting an infinite process short (a series, a limit, a step size) you take more terms or a smaller step using 5 terms of the series for exe^x
Rounding error storing numbers in finite precision (64-bit floats keep about 16 significant digits) you use more precision, or reorganise the formula 0.1 + 0.2 != 0.3

Mathematics deals with the first, computer arithmetic adds the second, and in real code you must balance them.

Taylor's remainder

The Taylor polynomial PnP_n of degree nn at aa leaves a remainder Rn(x)=f(x)−Pn(x)R_n(x) = f(x) - P_n(x). Taylor's theorem (Lagrange form) says that for some cc between aa and xx:

Rn(x)=f(n+1)(c)(n+1)!⋅(x−a)n+1R_n(x) = \frac{f^{(n+1)}(c)}{(n + 1)!} \cdot (x - a)^{n+1}

We do not know cc, but if ∣f(n+1)∣≤M\lvert f^{(n+1)} \rvert \le M on the interval, we get a guaranteed bound:

∣Rn(x)∣≤M⋅∣x−a∣n+1(n+1)!\lvert R_n(x) \rvert \le \frac{M \cdot \lvert x - a \rvert^{n+1}}{(n + 1)!}

The (n+1)!(n + 1)! in the denominator grows so fast that a few terms are enough near aa. For an alternating series with shrinking terms, the simpler rule from Convergence Tests applies: the error is at most the first omitted term.

Choosing the number of terms

How many terms of ex=∑xn/n!e^x = \sum x^n/n! give e=e1e = e^1 with error below 10−1010^{-10}? Every derivative of exe^x is ex≤e<3e^x \le e < 3 on [0,1][0, 1], so M=3M = 3:

python · try it yourself
import math

target = 1e-10
n = 0
while 3 / math.factorial(n + 1) >= target:      # bound M·1^(n+1)/(n+1)!
    n += 1
approx = sum(1 / math.factorial(k) for k in range(n + 1))
print("degree needed:", n)
print(f"bound  {3 / math.factorial(n + 1):.2e}")
print(f"actual {abs(approx - math.e):.2e}")
degree needed: 13
bound  3.44e-11
actual 1.23e-11

The real error is below the bound, as it must be. The bound is cautious (we used M=3M = 3 instead of the smallest possible value), which is the usual price of a guarantee.

When smaller is worse: the finite-difference trap

The derivative approximation f′(x)≈(f(x+h)−f(x))/hf'(x) \approx (f(x + h) - f(x)) / h has truncation error about (h/2)⋅∣f′′(x)∣(h/2) \cdot \lvert f''(x) \rvert, which shrinks with hh. But the subtraction f(x+h)−f(x)f(x + h) - f(x) loses digits when the two numbers are almost equal (catastrophic cancellation). The rounding error is about ε⋅∣f(x)∣/h\varepsilon \cdot \lvert f(x) \rvert/h with ε≈2.2×10−16\varepsilon \approx 2.2 \times 10^{-16}, which grows as hh shrinks.

python · try it yourself
import math
import matplotlib.pyplot as plt

x, exact = 1.0, math.cos(1.0)          # d/dx sin x = cos x
hs = [10.0 ** -k for k in range(1, 16)]
errors = [abs((math.sin(x + h) - math.sin(x)) / h - exact) for h in hs]
for h, err in zip(hs, errors):
    if h in (1e-1, 1e-4, 1e-8, 1e-12, 1e-15):
        print(f"h = {h:.0e}: error {err:.2e}")
best = min(zip(errors, hs))
print(f"best h = {best[1]:.0e}, error {best[0]:.2e}")

plt.figure(figsize=(6, 4))
plt.loglog(hs, errors, "o-")
plt.xlabel("step h")
plt.ylabel("error of the derivative")
plt.title("Truncation (right) vs rounding (left)")
h = 1e-01: error 4.29e-02
h = 1e-04: error 4.21e-05
h = 1e-08: error 2.97e-09
h = 1e-12: error 4.32e-05
h = 1e-15: error 1.48e-02
best h = 1e-08, error 2.97e-09

The total error is V-shaped on a log-log plot. The best step is near ε≈10−8\sqrt{\varepsilon} \approx 10^{-8}, where the two errors are about equal, and the best accuracy is only about 8 digits, not 16. This is why automatic differentiation (see Automatic Differentiation) is preferred over finite differences when training neural networks.

Rewriting a formula to avoid cancellation

Sometimes algebra removes the rounding problem. (1−cos⁡x)/x2(1 - \cos x)/x^2 tends to 1/21/2 as x→0x \to 0, but near 0 it subtracts two nearly equal numbers. The identity 1−cos⁡x=2sin⁡2(x/2)1 - \cos x = 2\sin^2(x/2) computes the same value without the subtraction:

python · try it yourself
import math

for x in [1e-3, 1e-6, 1e-8]:
    naive = (1 - math.cos(x)) / x**2
    stable = 2 * math.sin(x / 2) ** 2 / x**2
    print(f"x = {x:.0e}: naive {naive:.10f}   stable {stable:.10f}")

values = [0.1] * 10
total = 0.0
for v in values:                 # plain running sum
    total += v
print("running total     =", total)
print("math.fsum(values) =", math.fsum(values))
x = 1e-03: naive 0.4999999583   stable 0.4999999583
x = 1e-06: naive 0.5000444503   stable 0.5000000000
x = 1e-08: naive 0.0000000000   stable 0.5000000000
running total     = 0.9999999999999999
math.fsum(values) = 1.0

math.fsum tracks the rounding error of each addition and corrects for it. Since Python 3.12 the built-in sum also compensates for float rounding, and NumPy's sum uses pairwise summation for a similar reason; a hand-written += loop (in any language) does neither. Library functions like math.expm1(x) (=ex−1= e^x - 1) and math.log1p(x) (=ln⁡(1+x)= \ln(1 + x)) exist because the naive formulas cancel catastrophically for small xx.

Tip: A practical recipe: pick a method whose truncation error you can bound, choose the number of terms or the step so that bound meets your tolerance, and keep the tolerance well above 10−15×(size of the answer)10^{-15} \times (\text{size of the answer}), where rounding takes over.

Exercises

Exercise 1: Bound a sine approximation

Bound the error of sin⁡(0.5)≈0.5−0.53/6\sin(0.5) \approx 0.5 - 0.5^3/6.

Answer

At most 0.55/120≈2.6×10−40.5^5/120 \approx 2.6 \times 10^{-4} (the actual error is about 2.6×10−42.6 \times 10^{-4} too).

Solution

x−x3/6x - x^3/6 is also the degree-4 Taylor polynomial (the x4x^4 coefficient is 0), so use n=4n = 4. All derivatives of sin⁡\sin are bounded by M=1M = 1:

∣R4(0.5)∣≤1⋅0.55/5!=0.03125/120≈2.6×10−4\lvert R_4(0.5) \rvert \le 1 \cdot 0.5^5 / 5! = 0.03125 / 120 \approx 2.6 \times 10^{-4}

The series is also alternating, giving the same bound: the first omitted term 0.55/1200.5^5/120.

python · try it yourself
import math

print(f"{abs(math.sin(0.5) - (0.5 - 0.5**3 / 6)):.3e}  bound {0.5**5 / 120:.3e}")
2.589e-04  bound 2.604e-04

Exercise 2: Alternating bound

You approximate ln⁡(1.1)\ln(1.1) by 0.1−0.12/2+0.13/30.1 - 0.1^2/2 + 0.1^3/3. What is the largest the error can be?

Answer

0.14/4=2.5×10−50.1^4/4 = 2.5 \times 10^{-5}

Solution

The series x−x2/2+x3/3−x4/4+…x - x^2/2 + x^3/3 - x^4/4 + \ldots at x=0.1x = 0.1 alternates with decreasing terms, so the error is at most the first omitted term, 0.14/4=0.0000250.1^4/4 = 0.000025.

Exercise 3: Terms for e2e^2

How many terms (highest degree nn) of the series for exe^x guarantee an error below 10−610^{-6} at x=2x = 2? Use M=e2<7.4M = e^2 < 7.4.

Answer

Degree n=14n = 14 (15 terms).

Solution

We need 7.4⋅2n+1/(n+1)!<10−67.4 \cdot 2^{n+1}/(n + 1)! < 10^{-6}.

python · try it yourself
import math

n = 0
while 7.4 * 2 ** (n + 1) / math.factorial(n + 1) >= 1e-6:
    n += 1
approx = sum(2**k / math.factorial(k) for k in range(n + 1))
print(n, f"{abs(approx - math.exp(2)):.2e}")
14 2.86e-08

Exercise 4: The best step for central differences

The central difference (f(x+h)−f(x−h))/(2h)(f(x + h) - f(x - h)) / (2h) has truncation error proportional to h2h^2. Find the best power-of-ten hh for f=sin⁡f = \sin at x=1x = 1, and compare it with ε1/3≈6×10−6\varepsilon^{1/3} \approx 6 \times 10^{-6}.

Answer

The best hh is around 10−510^{-5} to 10−610^{-6}, with an error near 10−1110^{-11}, better than the forward difference's best of about 10−810^{-8}.

Solution

Truncation ∼h2\sim h^2, rounding ∼ε/h\sim \varepsilon/h: they balance near h≈ε1/3h \approx \varepsilon^{1/3}.

python · try it yourself
import math

errs = []
for k in range(1, 13):
    h = 10.0 ** -k
    d = (math.sin(1 + h) - math.sin(1 - h)) / (2 * h)
    errs.append((abs(d - math.cos(1)), h))
err, h = min(errs)
print(f"best h = {h:.0e}, error {err:.1e}")
best h = 1e-05, error 1.1e-11

Test yourself

Making the step hh in (f(x+h)−f(x))/h(f(x+h) - f(x))/h smaller and smaller in floating point eventually…

  1. gives the exact derivative
  2. makes the result worse, because rounding error grows like ε/h\varepsilon/h
  3. has no effect
  4. makes the truncation error grow

In the Lagrange remainder bound M⋅∣x−a∣n+1/(n+1)!M \cdot \lvert x - a \rvert^{n+1}/(n + 1)!, what is MM?

  1. The number of terms
  2. The largest value of ff on the interval
  3. A bound on ∣f(n+1)∣\lvert f^{(n+1)} \rvert between aa and xx
  4. Machine epsilon

Which is a truncation error?

  1. 0.1 + 0.2 printing 0.30000000000000004
  2. Stopping the series for exe^x after 6 terms
  3. Losing digits in 1−cos⁡(10−8)1 - \cos(10^{-8})
  4. Integer overflow