Applications of Taylor Polynomials
A Taylor polynomial is a cheap stand-in for a function, and Taylor's inequality says exactly how good it is. This lesson uses that to answer practical questions (how many terms, over what interval, with what error) and then looks inside a math library to see how exp is really computed: range reduction, a short polynomial, and Horner's method.
The basics of approximation error are in Approximation Error; the theory used here (Lagrange remainder, Taylor's inequality) is proved in Taylor and Maclaurin Series.
Three ways to bound the error
The -th degree Taylor polynomial of at is , and .
| Method | When it applies | Bound on |
|---|---|---|
| Taylor's inequality | always, if you can bound on the interval by | |
| Alternating series bound | the series at is alternating with decreasing terms | the first omitted term |
| Numerical check | as a sanity check, never as a proof | on a fine grid |
Worked example. Approximate by at on the interval .
import numpy as np
xs = np.linspace(8, 10, 2001)
T2 = 3 + (xs - 9) / 6 - (xs - 9) ** 2 / 216
err = np.abs(np.sqrt(xs) - T2)
M = 3 / 8 * 8 ** (-2.5)
print(f"Taylor bound : {M / 6:.3e}")
print(f"actual max : {float(err.max()):.3e} at x = {float(xs[err.argmax()]):.1f}")Taylor bound : 3.453e-04
actual max : 2.766e-04 at x = 8.0
The real worst error is about 80% of the bound, at , the end of the interval where is largest. On the other side ( near 10) the error is much smaller than the bound. Bounds are deliberately pessimistic: they replace by its worst value.
How many terms?
To hit a tolerance on , find the smallest with . For on ():
import math
d, eps = math.pi / 4, 1e-10
n = 0
while d ** (n + 1) / math.factorial(n + 1) >= eps:
n += 1
print(f"degree needed: {n}, bound {d ** (n + 1) / math.factorial(n + 1):.2e}")
x = d
T = sum((-1) ** k * x ** (2 * k + 1) / math.factorial(2 * k + 1) for k in range((n + 1) // 2))
print(f"T_{n}(π/4) = {T:.15f}\nsin(π/4) = {math.sin(x):.15f}")degree needed: 12, bound 6.95e-12
T_12(π/4) = 0.707106781179619
sin(π/4) = 0.707106781186547
The loop asks for degree 12, but sin has no term, so : the six odd-power terms already meet the tolerance, with the error bound of . Library authors exploit tricks like this to shave terms.
A physics application: the drop of the horizon
A point on the Earth's surface at arc distance from you lies below your tangent line by , where . Using :
The first term gives the surveyor's rule: about 7.8 cm at 1 km, 7.8 m at 10 km. The second term is ; since cos is alternating in its series, the error of the one-term rule is at most that, which for is about (under two micrometres). One Taylor term is all an engineer needs here, and the next term proves it.
In software: how libm computes exp
A math library cannot use the raw Maclaurin series of : for it would need hundreds of terms and would overflow on the way. Instead it combines three ideas.
- Range reduction. Write with , so . Then . Multiplying by is exact in binary floating point (it only changes the exponent bits:
math.ldexp). - A short polynomial on a tiny interval. On , Taylor's inequality with gives and , which is about one rounding step of a double near 1. So degree 12 is enough. Real libraries use a minimax polynomial instead (fitted to minimise the maximum error over the interval, Remez algorithm), which reaches the same accuracy with a lower degree. Taylor's error is tiny at and largest at the ends; minimax spreads it evenly.
- Horner's method. Evaluate with multiplications and additions, no powers, and good rounding behaviour.
import math
LN2 = math.log(2)
C = [1 / math.factorial(k) for k in range(13)] # Taylor coefficients of e^r
def horner(coeffs, r):
acc = 0.0
for c in reversed(coeffs):
acc = acc * r + c
return acc
def my_exp(x):
k = round(x / LN2)
r = x - k * LN2 # |r| <= ln2/2
return math.ldexp(horner(C, r), k) # 2^k · e^r
for x in (-20.5, -1.0, 0.1, 1.0, 10.0, 300.0, 700.0):
ours, ref = my_exp(x), math.exp(x)
print(f"x={x:7.1f} my_exp={ours:.16e} rel.err={abs(ours - ref) / ref:.1e}")x= -20.5 my_exp=1.2501528663867415e-09 rel.err=8.3e-16
x= -1.0 my_exp=3.6787944117144233e-01 rel.err=0.0e+00
x= 0.1 my_exp=1.1051709180756477e+00 rel.err=0.0e+00
x= 1.0 my_exp=2.7182818284590455e+00 rel.err=1.6e-16
x= 10.0 my_exp=2.2026465794806729e+04 rel.err=5.0e-16
x= 300.0 my_exp=1.9424263952412339e+130 rel.err=1.1e-14
x= 700.0 my_exp=1.0142320547350550e+304 rel.err=5.0e-14
For moderate the relative errors are at the level of double rounding (the spacing of doubles near 1 is about ). For and 700 they grow to about . The polynomial is not to blame: the stored is off by about , multiplies that by , and subtracts two nearly equal numbers near 700 whose own rounding is about . The resulting absolute error in becomes a relative error of the same size in . A production libm fixes exactly this: it splits into a high part with trailing zero bits (so is exact) and a tiny low part, so that is computed without that cancellation error. It also uses a table lookup for finer reduction () and a degree-5 or 6 minimax polynomial on the smaller interval. The same pattern of reduce, approximate with a polynomial, reconstruct is used for sin/cos (reduce modulo ), log (split off the exponent bits) and in GPU shader math.
Tip: NumPy's
np.polynomial.Chebyshev.interpolategives a near-minimax polynomial in one line; compare its maximum error with the Taylor polynomial of the same degree in Exercise 4.
Exercises
Exercise 1: A bound by hand
Use Taylor's inequality to bound the error of for at on .
Answer
.
Solution
The actual error at is , below the bound.
Exercise 2: Alternating bound
How large can be if must be accurate to ?
Answer
(about ).
Solution
For the sin series is alternating with decreasing terms, so the error is at most the next term :
import math
x = (1.2e-3) ** 0.2
print(f"x = {x:.4f} rad = {math.degrees(x):.1f}°, error = {abs(math.sin(x) - (x - x**3 / 6)):.2e}")x = 0.2605 rad = 14.9°, error = 9.98e-06
Exercise 3: Horner by hand
Evaluate at with Horner's scheme. How many multiplications does it use, compared with the naive method?
Answer
. Horner uses 4 multiplications; computing each term separately uses 8.
Solution
check: 2·81 − 3·27 + 3 − 5 = 162 − 81 + 3 − 5 = 79
coefficients: 2, −3, 0, 1, −5
2
2·3 − 3 = 3
3·3 + 0 = 9
9·3 + 1 = 28
28·3 − 5 = 79Naively, needs 4 multiplications, needs 3 and needs 1: 8 in total. For degree , Horner needs , the naive way about .
Exercise 4: Taylor versus near-minimax
On , compare the maximum relative error of the degree-6 Taylor polynomial of with a degree-6 Chebyshev interpolant (np.polynomial.Chebyshev.interpolate).
Answer
Taylor: about ; Chebyshev: about , roughly 64 times more accurate at the same degree.
Solution
Taylor concentrates its accuracy at ; the Chebyshev interpolant spreads it across the interval, which is close to the minimax ideal.
import math
import numpy as np
from numpy.polynomial import Chebyshev
h = math.log(2) / 2
r = np.linspace(-h, h, 5001)
taylor = sum(r**k / math.factorial(k) for k in range(7))
cheb = Chebyshev.interpolate(np.exp, 6, domain=[-h, h])
t_err = float(np.max(np.abs(taylor - np.exp(r)) / np.exp(r)))
c_err = float(np.max(np.abs(cheb(r) - np.exp(r)) / np.exp(r)))
print(f"Taylor deg 6 : {t_err:.2e}")
print(f"Chebyshev deg 6: {c_err:.2e} ({t_err / c_err:.0f}x better)")Taylor deg 6 : 1.61e-07
Chebyshev deg 6: 2.53e-09 (64x better)
Test yourself
Why does a math library reduce to before evaluating a polynomial for ?
- Because is rational
- So the polynomial only has to be accurate on a tiny interval, needing few terms
- To avoid using multiplication
- Because is periodic
Taylor's inequality gives on an interval. The true maximum error is 0.004. Is that a contradiction?
- Yes, the bound must be exact
- Yes, the true error must be larger
- No: the bound uses the worst-case value of , so it is usually pessimistic
- No: Taylor's inequality only works at the centre
What does a minimax polynomial minimise?
- The maximum error over the whole interval
- The error at the centre
- The number of coefficients
- The sum of the coefficients