Tutorials › Calculus II › Cancellation and Roundoff in Taylor Evaluation

Taylor Methods in Computation · Tutorial 878 of 998

Cancellation and Roundoff in Taylor Evaluation

See why nearly cancelling Taylor terms can make a result unreliable, and how to identify whether the main error comes from arithmetic or from truncating the series.

Advanced 11 min read

What You'll Learn

  • Recognize cancellation when Taylor-polynomial terms nearly cancel.
  • Estimate the sensitivity of a sum using its cancellation condition number.
  • Separate polynomial truncation error from floating-point evaluation error.
  • Trace how finite-precision arithmetic can corrupt a small result.
  • Compare direct evaluation with a stable reformulation or higher precision.

Why accurate Taylor approximations can give inaccurate computations

A Taylor polynomial may approximate a function extremely well and still produce a poor computed answer. The issue is that evaluation requires arithmetic on the polynomial’s terms, and those operations are performed with finite precision. If large terms nearly cancel, small rounding errors in them can become large relative to the small result.

It is useful to distinguish two sources of error. Truncation error comes from replacing a function by a Taylor polynomial of finite degree. Roundoff error comes from evaluating that polynomial using finite-precision arithmetic. Improving one does not necessarily improve the other: increasing the degree addresses truncation, while extra precision or a more suitable formula addresses roundoff.

Key distinction: A Taylor remainder measures the difference between the function and its polynomial. It does not, by itself, measure the difference between the polynomial and the value produced by a computer.

Cancellation and sensitivity

Suppose a quantity is written as a sum \(s=a_1+\cdots+a_m\). Cancellation occurs when positive and negative terms of substantial size combine to give a much smaller result. A useful measure of the sum’s sensitivity to small relative changes in its terms is its cancellation condition number:

$$ \kappa_{\mathrm{sum}} = \frac{|a_1|+\cdots+|a_m|}{|a_1+\cdots+a_m|}, \qquad a_1+\cdots+a_m\ne 0. $$

If the terms are all of the same sign, this ratio is \(1\). If the terms nearly cancel, the denominator is small and the ratio can be large. A large value warns that even small relative errors in the terms may cause a large relative error in the sum.

In a standard floating-point model, an individual arithmetic operation is often described by \(\operatorname{fl}(a\mathbin{\circ}b)=(a\mathbin{\circ}b)(1+\delta)\), where \(\circ\) is an operation and \(|\delta|\le u\). Here \(u\) is the unit roundoff, and the model assumes the operation does not overflow or enter a range where underflow makes the model inappropriate. This is an approximation to how floating-point arithmetic behaves, not a guarantee that every calculation has the same error.

How cancellation amplifies small errors

Consider terms \(a_i\) that are each perturbed by a small relative amount \(\delta_i\), with \(|\delta_i|\le u\). The perturbed sum differs from the exact sum by \(\sum_i a_i\delta_i\). The triangle inequality gives a bound on that difference:

$$ \left|\sum_i a_i\delta_i\right| \le \sum_i |a_i|\,|\delta_i| \le u\sum_i |a_i|. $$

When the exact sum \(s=\sum_i a_i\) is nonzero, dividing by \(|s|\) gives the relative-error estimate

$$ \frac{\left|\sum_i a_i\delta_i\right|}{|s|} \le u\,\kappa_{\mathrm{sum}}. $$

This derivation explains the role of cancellation: the same scale of relative error in the individual terms can have a much bigger relative effect on their sum when \(\kappa_{\mathrm{sum}}\) is large. In an actual calculation, intermediate rounding in multiplications and additions can add further error, so this estimate is a diagnostic rather than a complete error bound for every evaluation procedure.

Worked Example: A Taylor polynomial with nearly cancelling terms

Worked Example: A quadratic Taylor polynomial near a root

Let \(f(x)=(1-x)^2\). Its Taylor polynomial about \(x=0\) is exactly \(P_2(x)=1-2x+x^2\): expanding the function gives \(1-2x+x^2\), and there are no higher-degree terms. Evaluate it at \(x=0.9999999\), first using seven-significant-digit decimal arithmetic, rounding each operation to seven significant digits.

The exact function value is

$$ f(0.9999999) =(1-0.9999999)^2 =(0.0000001)^2 =0.00000000000001 =10^{-14}. $$

Now evaluate the expanded polynomial. First compute its terms, rounding the product \(2x\) and the product \(x^2\) to seven significant digits:

$$ 2x=2(0.9999999)=1.9999998 \longrightarrow 2.000000, $$ $$ x^2=(0.9999999)^2=0.99999980000001 \longrightarrow 0.9999998. $$

Combining the terms from left to right with the same rounding rule gives

$$ 1-2.000000=-1.000000, $$ $$ -1.000000+0.9999998=-0.0000002=-2\times10^{-7}. $$

The computed result is \(-2\times10^{-7}\), although the true result is \(10^{-14}\). The absolute error is \(2.0000001\times10^{-7}\), and the relative error is approximately \(2.0000001\times10^7\). The polynomial itself is exact, so its truncation error is zero; the failure here is due to finite-precision evaluation.

The terms are poorly conditioned as a sum. Their cancellation condition number is

$$ \kappa_{\mathrm{sum}} = \frac{|1|+|{-2x}|+|x^2|}{|1-2x+x^2|} = \frac{1+1.9999998+0.99999980000001}{10^{-14}} \approx 4\times10^{14}. $$

In contrast, evaluate the equivalent factored expression using the same seven-significant-digit arithmetic:

$$ 1-x=1-0.9999999=0.0000001, $$ $$ (1-x)^2=(0.0000001)^2=10^{-14}. $$

In this particular decimal-arithmetic example, the subtraction and square retain the needed result. Substitution into the original function confirms that the factored value is correct: \(f(0.9999999)=10^{-14}\). Equivalent formulas need not behave equally well in finite precision.

Separating truncation error from roundoff error

Let \(P_n(x)\) be the degree-\(n\) Taylor polynomial used to approximate \(f(x)\), and let \(\widehat{P}_n(x)\) denote the result actually computed in finite precision. Then the total error can be decomposed exactly:

$$ \widehat{P}_n(x)-f(x) = \bigl(P_n(x)-f(x)\bigr) + \bigl(\widehat{P}_n(x)-P_n(x)\bigr). $$

The first parenthesis is the polynomial’s truncation error; the second is the evaluation error, which includes roundoff. A Taylor remainder estimate bounds the first quantity. It cannot establish that the second is small. This is why a degree chosen to meet an approximation tolerance may still be unsuitable if the resulting polynomial is evaluated in a cancellation-prone way.

Practical rule: Check both the Taylor remainder and the numerical evaluation. A small remainder does not certify a reliable computed value when the polynomial terms nearly cancel.

Worked Example: Subtracting nearly equal quantities

Worked Example: Evaluating \(e^x-1\) near zero

Consider \(e^x-1\) at \(x=10^{-10}\). The degree-two Taylor polynomial for \(e^x\) at zero is \(P_2(x)=1+x+x^2/2\). Suppose calculations use decimal arithmetic with eight significant digits and round after each operation.

Evaluating \(P_2(x)\) begins with

$$ 1+x=1+10^{-10}=1.0000000001 \longrightarrow 1.0000000. $$

The quadratic term is

$$ \frac{x^2}{2} = \frac{(10^{-10})^2}{2} = 5\times10^{-21}. $$

Adding that term to \(1.0000000\) still rounds to \(1.0000000\). Subtracting \(1\) therefore produces the computed result \(0\). The subtraction loses the small difference that was the quantity of interest.

Instead, use the algebraically equivalent Taylor polynomial for \(e^x-1\), obtained by subtracting the constant term before evaluation:

$$ P_2(x)-1=x+\frac{x^2}{2} =10^{-10}+5\times10^{-21} =1.00000000005\times10^{-10}. $$

Rounded to eight significant digits, this is \(1.0000000\times10^{-10}\), rather than zero. For a check against the function, Taylor’s theorem gives a remainder after degree two of \(e^\xi x^3/6\) for some \(\xi\) between \(0\) and \(10^{-10}\). Its magnitude is less than \(2\times10^{-31}\), since \(e^\xi<2\) on this interval. Thus the polynomial approximation has a tiny truncation error, while the direct finite-precision calculation still returned zero. The reformulation avoids the damaging final subtraction.

Worked Example: When more precision does not fix truncation

Worked Example: Approximating \(e^{0.1}\) with degree two

Use \(P_2(x)=1+x+x^2/2\) to approximate \(e^{0.1}\). The polynomial value is

$$ P_2(0.1) = 1+0.1+\frac{(0.1)^2}{2} = 1+0.1+0.005 = 1.105. $$

The function value is approximately \(e^{0.1}=1.105170185\ldots\), so the actual error is approximately

$$ |e^{0.1}-P_2(0.1)| \approx 1.105170185-1.105 = 0.000170185. $$

Taylor’s theorem gives the remainder \(R_2=e^\xi(0.1)^3/6\) for some \(0<\xi<0.1\). Since \(e^\xi<e^{0.1}<1.106\),

$$ |R_2| < \frac{1.106(0.001)}{6} < 0.00018434. $$

The actual error is within this bound. If the polynomial is evaluated accurately, increasing arithmetic precision will leave the approximation near \(1.105\); the remaining difference from \(e^{0.1}\) is truncation error. To reduce that error, use a higher-degree Taylor polynomial or another approximation with a suitable remainder bound. Substitution into \(P_2\) verifies the computed polynomial value: \(1+0.1+0.005=1.105\).

Choosing a reliable evaluation strategy

When a Taylor calculation produces a result much smaller than its individual terms, pause before treating the displayed digits as trustworthy. The following checks help identify the source of trouble:

1
Estimate the truncation error.
Use a valid Taylor remainder bound to determine how close the polynomial itself is to the function.
2
Inspect the terms being combined.
If large positive and negative terms produce a small sum, the sum’s cancellation condition number may be large.
3
Check the evaluation procedure.
Compare the computed value with an algebraically equivalent form that avoids subtracting nearly equal quantities, when such a form is available.
4
Compare at higher precision.
If the result changes substantially as precision increases, roundoff is significant. If it stabilizes but remains different from the function, truncation may dominate.

Neither reformulation nor extra precision is a substitute for checking the approximation error. A stable evaluation of a low-degree polynomial can still be a poor approximation to the function. Likewise, a very accurate Taylor approximation can be spoiled by an unstable evaluation formula.

Check Your Understanding

For each question, identify what the given information does—and does not—tell you about the computed result.

  1. What does a large cancellation condition number indicate about a sum?
  2. Why does a small Taylor remainder bound not guarantee that a computed polynomial value is accurate?
  3. For \(f(x)=(1-x)^2\), why can \(1-2x+x^2\) be less reliable near \(x=1\) than \((1-x)^2\)?
  4. In the calculation of \(e^x-1\) for very small \(x\), why can evaluating \(e^x\) and then subtracting \(1\) lose the desired result?
  5. If a computed answer changes when arithmetic precision is increased, which error source should you investigate? If it remains stable but disagrees with the function, what should you investigate?

Test yourself: Calculus II final exam practice

Ready to check what you know? Work a full-length practice final, then open the complete worked solutions.

Want all 1,114 practice problems?

This tutorial covers one topic. Calc Pass covers the full Calculus AB & BC curriculum (the core of Calculus I and II): full review, every practice question, and step-by-step solutions to each one.

QR code to download the Calc Pass app

Get the Calc Pass App

Scan to download and practice on the go, with instant scoring.

Calc Pass Volume 1: 1,114 Practice Questions book cover

Vol. 1: 1,114 Questions

The complete practice question bank, organized by AB & BC topic.

View on Amazon
Calc Pass Volume 2: Solutions Manual book cover

Vol. 2: Solutions Manual

Fully worked solutions to every question in Volume 1.

View on Amazon