🎯Computer Arithmetic and Error
Taylor Series and Truncation Error
Taylor's theorem with remainder, order of accuracy, the finite-difference sweet spot, and how libraries evaluate functions
Hewlett-Packard, 1972
In 1972 Hewlett-Packard releases the HP-35, the first scientific calculator that fits in a shirt pocket. It does sine, cosine, tangent, logarithms and exponentials — and makes the slide rule obsolete within a few years.
Its processor cannot multiply in hardware. It works one decimal digit at a time, adding and shifting.
So how does it compute ?
Not by looking it up: no table would fit. It uses an algorithm called CORDIC, invented by Jack Volder in 1959 for the navigation computer of the B-58 bomber, which turns a trigonometric function into a sequence of additions and shifts. Your laptop does something different — range reduction followed by a short polynomial — but both share one idea. Every elementary function a computer evaluates is an approximation with a known, deliberately accepted error.
This lesson is about that deliberate error: where it comes from, how big it is, and how it fights rounding error for control of your answer.
Numerical methods replace something infinite or continuous — a series, a limit, an integral, a differential equation — by something finite. The error that this substitution makes, even in exact arithmetic, is called truncation error (or discretisation error). It is the error of the method. Rounding error, from the last two lessons, is the error of the machine.
The main tool for measuring truncation error is one theorem.
Taylor's theorem with the Lagrange remainder. If f has n+1 continuous derivatives, the error of the degree-n polynomial is exactly R_n for some unknown ξ — so it can be bounded by the largest |f^(n+1)| on the interval.
Derivation
Where Taylor's coefficients come from
Look for the polynomial in that behaves like near :
Ask it to match and its first derivatives at . Differentiating times kills every term below , turns into , and leaves every higher term still multiplied by a power of , which vanishes at . So
That is the whole origin of the in every Taylor series.
The remainder needs one more idea. For it is the mean value theorem: somewhere between and the slope of equals the slope of the chord, so . The Lagrange form for general is the same statement one level up, proved by applying Rolle's theorem repeatedly to the difference between and its Taylor polynomial — the same argument we will use for interpolation in lesson 7-1.
💡Order of accuracy and big-O
We write : as , the error shrinks like times a constant.
This is the single most useful fact about a numerical method. A method of order p has error . Halve the step and the error falls by : by 2 for first order, by 16 for fourth order. Plot against and the slope is — that is how you check a method's order experimentally, and how you catch a bug that silently turns a second-order code into a first-order one.
📐How many terms does sin need?
For about 0 every derivative is bounded by 1, so the degree- Taylor polynomial has error at most .
At , eight terms (degree 15) give an error of about — far beyond double precision. At the same polynomial is wrong by about , and at by almost : the series stops while its terms are still growing, before the factorials catch up. Adding terms until they shrink does not rescue it either. At the largest term is about , and cancelling terms that size down to an answer below 1 destroys every digit.
Real libraries therefore reduce the argument first. Using symmetries they map any to with , then evaluate a short polynomial in . The lab builds this: range reduction plus an 8-term polynomial matches
math.sin to about on .Far better an approximate answer to the right question, which is often vague, than an exact answer to the wrong question, which can always be made precise.
The derivative and its sweet spot. Taylor's theorem gives the simplest numerical derivative directly. Rearranging :
Forward difference (first order) and central difference (second order): the odd terms of the two Taylor expansions cancel in the central formula.
Derivation
Both difference formulas from Taylor's theorem
Expand a step forward and a step back:
Forward difference. Rearrange the first line alone:
The leading error is : proportional to , first order.
Central difference. Subtract the second line from the first. The terms cancel, the terms cancel, and the odd terms double:
The leading error is : second order. Symmetry bought a whole order of accuracy for the same two function evaluations.
Truncation error says: make small. But the numerator subtracts two nearly equal numbers — the cancellation of the last lesson. Each value of carries a rounding error of about , and dividing their difference by amplifies it to about . The total error of the forward difference is therefore roughly
Truncation falls with ; rounding grows as shrinks. The minimum is near , with an error around — half the digits of double precision are gone. The central difference does better, with an optimum near and an error near . Push to and the answer is garbage.
Derivation
The best step, found by calculus
Write , and near .
Forward difference. Truncation contributes . Each of and carries a rounding error up to about , so their difference carries up to , and dividing by gives . Minimise the total:
At the two parts are equal and . For a function with and of order 1, and : half of the sixteen digits.
Central difference. Truncation is ; the rounding error is now divided by :
With : , and the error there is — about eleven digits. The general lesson: a method of order balanced against rounding has its best step near and loses about of the digits.
🕰A derivative with no cancellation
In 1967 Lyness and Moler found a trick that sidesteps the V entirely when can be evaluated for complex arguments. Expand : the real part carries , the imaginary part carries . So involves no subtraction, and gives a derivative accurate to the last bit. It is now used to verify aerodynamic and structural sensitivities in large engineering codes.
Derivation
Why the complex step works
Taylor's theorem holds for a step in any direction of the complex plane, provided is smooth (analytic) there. Step by , an imaginary amount, and use , :
For real and a function that is real on the real line, the real and imaginary parts separate cleanly:
Divide the imaginary part by :
Second order, like the central difference — but nothing was subtracted. The only rounding error is the one made in computing itself, so can be made as small as we like: at the truncation term is and the derivative is correct to the last bit.
💡Where you will use this
Every time step of an ODE solver in Orbital Mechanics, Attitude Dynamics and Rocket trajectories is a truncated Taylor series; its order decides your step size. Jacobians for Newton's method in Rocket Propulsion and Power Systems are often finite differences, and choosing is exactly this lesson. Finite-difference and finite-element meshes in Structures, Heat Transfer and Geotechnical Engineering are described by their order of accuracy.
📚Further reading
- Michael Heath, Scientific Computing: An Introductory Survey (2nd ed.; SIAM Classics 2018), chapter 1 (truncation and rounding error) and the numerical-differentiation sections of chapter 8.
- Jean-Michel Muller, Elementary Functions: Algorithms and Implementation (3rd ed., Birkhäuser, 2016) — range reduction, polynomial approximation and CORDIC, as libraries really do them.
- William Squire and George Trapp, 'Using complex variables to estimate derivatives of real functions', SIAM Review 40(1), 110–112 (1998) — the three-page paper that revived the complex step.
↓But there is a problem
We now know where error comes from: rounding in every operation, truncation in every method. Good algorithms keep both small.
But there is a third ingredient, and no algorithm can fix it.
In 1961 a meteorologist at MIT re-ran a weather simulation from the middle, typing in a number from a printout: 0.506 instead of the 0.506127 the machine had held. A difference in the fourth decimal. Within two months of simulated weather the two runs had nothing in common. The machine had made no mistake, and neither had his algorithm. The problem itself was amplifying the error.