5. Sequences, series and Taylor polynomials

A machine adds and multiplies. It has no fast instruction for \(e^x\) or \(\cos x\). To compute them we replace each one by a polynomial, and to trust the result we need to know how far off the polynomial is. This chapter does both. An infinite sum (a series) is the limit of a running total; a Taylor polynomial is the polynomial that matches a function's derivatives at a point; its remainder says how wrong it is.

The rest of the book uses the results constantly. The limit \((1 - \cos x)/x^2 \to 1/2\) of section 1.3 is explained here. The paper's risk score contains \(\cosh \delta - 1\), which for a small logit spread \(\delta\) is \(\delta^2/2\) to within a factor \(1 + \delta^2/12\) (section 5.4); section 19.4 leans on that. And section 5.6 shows why the polynomial inside the exponential of section 4.7 is chosen the way it is.

5.1 Convergence

A sequence \(a_1, a_2, \dots\) converges when its terms settle on a value (section 1.5). A series \(\sum_{n=1}^{\infty} a_n\) is a sequence in disguise: the sequence of its partial sums

\[ S_N = \sum_{n=1}^{N} a_n = a_1 + a_2 + \dots + a_N, \]

the running total after \(N\) terms.

Definition. The series \(\sum a_n\) converges to \(S\) if its partial sums do: \(S_N \to S\) as \(N \to \infty\). Then we write \(\sum_{n=1}^{\infty} a_n = S\). Otherwise it diverges.

Three series side by side: the harmonic series \(1 + \tfrac12 + \tfrac13 + \dots\), the squares \(1 + \tfrac14 + \tfrac19 + \dots\), and the alternating harmonic series \(1 - \tfrac12 + \tfrac13 - \dots\). Their partial sums, computed in doubles:

\(N\)\(\sum 1/n\)\(\ln N + 0.5772\)\(\sum 1/n^2\)\(\sum (-1)^{n+1}/n\)
\(10\)2.9289682.8798011.54976773120.6456349206
\(10^3\)7.4854717.4849711.64393456670.6926474306
\(10^5\)12.09014612.0901411.64492406690.6931421806
\(10^7\)16.69531116.6953111.64493396680.6931471306
limitnone1.64493406680.6931471806

The squares settle on \(\pi^2/6\) and the alternating sum on \(\ln 2\) (section 5.3 shows why). The harmonic series does not settle: it grows like \(\ln N + 0.5772\), slowly but without bound, though its terms go to 0. So:

Fact. If \(\sum a_n\) converges then \(a_n \to 0\). The converse is false: \(\sum 1/n\) diverges [openstax].

The harmonic series diverges because its terms do not shrink fast enough. Group them: \(\tfrac13 + \tfrac14 > \tfrac12\), \(\tfrac15 + \dots + \tfrac18 > \tfrac12\), \(\tfrac19 + \dots + \tfrac1{16} > \tfrac12\), and so on: infinitely many halves. The squares shrink fast enough; the error after \(N\) terms is about \(1/N\) (the table: \(1.6449340668 - 1.6439345667 \approx 10^{-3}\) at \(N = 1000\)). The alternating series converges for a different reason, cancellation: its partial sums step over and under the limit, and the error is at most the next term, here about \(1/(2N)\).

A series whose absolute values \(\sum |a_n|\) converge is absolutely convergent; it converges, and to the same sum in any order. The alternating harmonic series is not: \(\sum 1/n\) diverges. Reorder its terms (say, two positive terms per negative one) and it converges to a different number [spivak]. Every series in the rest of the book is absolutely convergent, so order does not matter in exact arithmetic.

In the machine. In floating point order always matters. In float the harmonic sum stops growing at \(n = 2^{21} = 2097152\), with sum \(15.403683\): \(1/n\) is then less than half an ulp of the total, and s + 1.0f/n == s. The math says the sum goes to infinity; the machine says 15.4. Adding small terms first, or carrying the lost bits (Kahan summation, section 14.5), fixes much of this.

5.2 Geometric series

The series where each term is the last times a fixed ratio \(x\):

\[ 1 + x + x^2 + x^3 + \dots \]

Its partial sums have a closed form. Let \(S_N = 1 + x + \dots + x^{N-1}\). Multiply by \(x\): \(x S_N = x + x^2 + \dots + x^N\). Subtract, and everything but the ends cancels: \(S_N - x S_N = 1 - x^N\). So for \(x \ne 1\)

\[ S_N = \sum_{n=0}^{N-1} x^n = \frac{1 - x^N}{1 - x}. \]

If \(|x| < 1\) then \(x^N \to 0\), and

Theorem (geometric series). For \(|x| < 1\), \(\displaystyle \sum_{n=0}^{\infty} x^n = \frac{1}{1 - x}\). For \(|x| \ge 1\) the series diverges.

The error after \(N\) terms is \(\dfrac{1}{1-x} - S_N = \dfrac{x^N}{1 - x}\): it shrinks by the factor \(x\) per term. Close to \(x = 1\) that is slow:

\(x\)\(S_{10}\)\(S_{100}\)\(S_{1000}\)\(1/(1-x)\)
0.51.9980469222
−0.50.666015620.666666670.666666670.666667
0.96.51321569.99973441010
0.999.561792563.39676699.995683100
1.0110.462213170.481382095815.6−100

At \(x = 0.99\), 1000 terms still miss by \(0.99^{1000}/0.01 \approx 0.004\). At \(x = 1.01\) the partial sums explode, while the formula \(1/(1-x) = -100\) cheerfully returns a number: a closed form is only valid where the series converges.

The geometric series is behind a trick you may have used. rcpps returns \(y_0 \approx 1/a\) to 12 bits; a Newton step \(y_1 = y_0 (2 - a y_0)\) doubles that (section 2.6). Why: write \(a y_0 = 1 - e\), with \(e\) the small relative error. Then

\[ \frac{1}{a} = \frac{y_0}{1 - e} = y_0 (1 + e + e^2 + \dots), \qquad y_1 = y_0 (2 - a y_0) = y_0 (1 + e). \]

The Newton step keeps the first two terms of the geometric series, so what it misses starts at \(e^2\): its relative error is exactly \(1 - a y_1 = 1 - (1-e)(1+e) = e^2\). Starting from \(y_0 = 0.33\) for \(a = 3\) (\(e = 0.01\)), the errors in doubles are \(10^{-4}\), \(10^{-8}\), then \(2.2 \times 10^{-16}\): squared each step until they reach rounding level, \(2^{-53}\), where they stop.

5.3 Power series

Let the terms of a series depend on \(x\) through powers: \(\sum_{n=0}^{\infty} c_n x^n = c_0 + c_1 x + c_2 x^2 + \dots\). That is a power series: a polynomial of infinite degree. The geometric series is the one with every \(c_n = 1\). Where it converges, it defines a function of \(x\).

Theorem (radius of convergence). For each power series there is an \(R\), \(0 \le R \le \infty\), such that the series converges absolutely for \(|x| < R\) and diverges for \(|x| > R\). When the limit exists, \(R = \lim_{n \to \infty} |c_n / c_{n+1}|\). Inside \(|x| < R\), the function it defines can be differentiated and integrated term by term [openstax].

The formula for \(R\) comes from comparing with a geometric series: the ratio of successive terms is \(|c_{n+1} x / c_n|\), and if that ends up below 1 the terms shrink at least geometrically.

Two power series matter most here.

The exponential. \(\displaystyle \exp x = \sum_{n=0}^{\infty} \frac{x^n}{n!} = 1 + x + \frac{x^2}{2} + \frac{x^3}{6} + \dots\) Here \(c_n / c_{n+1} = (n+1)!/n! = n + 1 \to \infty\), so \(R = \infty\): it converges for every \(x\). Differentiate term by term: \(x^n/n!\) becomes \(n x^{n-1}/n! = x^{n-1}/(n-1)!\), the previous term. So the series is its own derivative, the defining property of \(\exp\) (section 4.1, and section 6.2 for the equation \(y' = y\)).

The logarithm. Put \(-t\) in the geometric series: \(\dfrac{1}{1+t} = 1 - t + t^2 - t^3 + \dots\) for \(|t| < 1\). Integrate both sides from 0 to \(x\) (section 3.4), term by term:

\[ \ln(1 + x) = x - \frac{x^2}{2} + \frac{x^3}{3} - \frac{x^4}{4} + \dots, \qquad -1 < x \le 1. \]

\(R = 1\), as for the geometric series it came from. At \(x = 1\) it is the alternating harmonic series of section 5.1, which is why that sum is \(\ln 2\).

Converging is not the same as converging usefully. Errors of the partial sums up to degree \(n\), computed in doubles, against the libm value:

\(n\)\(e^{1}\)\(e^{10}\)\(e^{-20}\)\(\ln 1.5\)\(\ln 2\)\(\ln 3\)
4\(-9.9 \times 10^{-3}\)\(-2.1 \times 10^{4}\)\(5.5 \times 10^{3}\)\(-4.4 \times 10^{-3}\)\(-0.11\)\(-2.4\)
16\(-2.7 \times 10^{-15}\)\(-6.0 \times 10^{2}\)\(1.7 \times 10^{7}\)\(-3.0 \times 10^{-7}\)\(-0.030\)\(-2.7 \times 10^{3}\)
320\(-1.6 \times 10^{-4}\)\(6.2 \times 10^{5}\)\(-2.4 \times 10^{-12}\)\(-0.015\)\(-8.9 \times 10^{7}\)
640\(-3.6 \times 10^{-12}\)\(3.5 \times 10^{-7}\)\(-1.1 \times 10^{-16}\)\(-0.0078\)\(-1.9 \times 10^{17}\)

Read the columns. \(\exp(1)\): degree 18 reaches rounding level. \(\exp(10)\): converges, but the terms grow until \(n \approx 10\) before they shrink, so it needs degree 46 (nterms.c). \(\exp(-20)\): the terms alternate in sign and peak near \(20^{20}/20! \approx 4 \times 10^7\); the sum, \(2 \times 10^{-9}\), is what is left after they cancel, and the rounding errors of the big terms (about \(4 \times 10^7 \times 10^{-16}\)) are larger than the answer. Even at degree 100 the sum is \(6.1 \times 10^{-9}\) against the true \(2.06 \times 10^{-9}\). Computing \(1/\exp(20)\) instead, a sum of positive terms, gives full accuracy. \(\ln(1.5)\) converges fast, \(\ln 2\) (the edge, \(x = 1\)) like \(1/n\), and \(\ln 3\) (\(x = 2\), outside the radius) not at all.

The lesson for computing: use a power series only on a small interval around its center, where the terms fall fast. Section 5.6 shows how a library arranges for the argument to be small.

5.4 Taylor polynomials

Given a function, which polynomial best imitates it near 0? Match the value, the slope, the curvature, and so on. If \(p(x) = c_0 + c_1 x + c_2 x^2 + \dots + c_n x^n\), differentiating \(k\) times and setting \(x = 0\) kills every term but one: \(p^{(k)}(0) = k!\, c_k\). Setting \(p^{(k)}(0) = f^{(k)}(0)\) for \(k = 0, \dots, n\) gives the coefficients.

Definition. The Taylor polynomial of degree \(n\) of \(f\) at \(a\) is

\[ P_n(x) = \sum_{k=0}^{n} \frac{f^{(k)}(a)}{k!} (x - a)^k = f(a) + f'(a)(x-a) + \frac{f''(a)}{2}(x-a)^2 + \dots \]

At \(a = 0\) it is also called the Maclaurin polynomial. Letting \(n \to \infty\) gives the Taylor series.

\(P_1\) is the tangent line, the linear approximation of section 2.4; \(P_2\) adds the curvature. The table of the functions this book uses, all at \(a = 0\):

\[ \begin{aligned} e^x &= 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \frac{x^4}{4!} + \dots & &\text{all } x \\ \sin x &= x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + \dots & &\text{all } x \\ \cos x &= 1 - \frac{x^2}{2!} + \frac{x^4}{4!} - \frac{x^6}{6!} + \dots & &\text{all } x \\ \sinh x &= x + \frac{x^3}{3!} + \frac{x^5}{5!} + \frac{x^7}{7!} + \dots & &\text{all } x \\ \cosh x &= 1 + \frac{x^2}{2!} + \frac{x^4}{4!} + \frac{x^6}{6!} + \dots & &\text{all } x \\ \ln(1 + x) &= x - \frac{x^2}{2} + \frac{x^3}{3} - \frac{x^4}{4} + \dots & &-1 < x \le 1 \end{aligned} \]

Where they come from:

-6 -4 -2 2 4 6 n=1 n=3 n=5 n=7 n=9 n=11 sin x
Figure 5.1. \(\sin x\) (thick) and its Taylor polynomials at 0 of degree 1, 3, 5, 7, 9 and 11. Each one follows the curve a little farther before it turns away; every one of them eventually runs off to \(\pm\infty\), while \(\sin\) stays between \(-1\) and 1.

Each row of the table was checked numerically: \((f(x) - P_n(x))/x^{n+1}\) tends to the next coefficient as \(x \to 0\).

Two expansions the book uses

Subtract the cosine series from 1 and divide by \(x^2\):

\[ \frac{1 - \cos x}{x^2} = \frac{1}{2} - \frac{x^2}{24} + \frac{x^4}{720} - \dots = \frac{1}{2} - \frac{x^2}{24} + O(x^4). \]

That is the limit \(1/2\) of section 1.3, with the rate at which it is approached: the error of \(1/2\) is \(x^2/24\), the big-O statement of section 1.6.

The cosh series, for a spread \(\delta\):

\[ \cosh \delta = 1 + \frac{\delta^2}{2} + \frac{\delta^4}{24} + \frac{\delta^6}{720} + \dots, \qquad \cosh \delta - 1 = \frac{\delta^2}{2}\left(1 + \frac{\delta^2}{12} + \dots\right). \]

All terms are positive, so \(\cosh \delta \ge 1 + \delta^2/2\) for every \(\delta\), and \(\cosh \delta - 1\) is \(\delta^2/2\) up to a relative error of about \(\delta^2/12\): at \(\delta = 0.1\), 0.08%. In the paper's risk score (section 19.7) the cluster's term is \(\cosh \delta_C - 1\): for small spreads, the error grows with the square of the spread. Halve \(\delta_C\) and that part of the bound drops by 4. Its other factor, \(\tanh(\delta/2)\), has the series \(\tanh x = x - x^3/3 + 2x^5/15 - \dots\), so \(\tanh(\delta/2) \approx \delta/2\): linear in the spread.

Numerically, with the left sides computed without cancellation through \(1 - \cos x = 2\sin^2(x/2)\) and \(\cosh x - 1 = 2\sinh^2(x/2)\): at \(x = 0.1\), \(\big(\tfrac{1-\cos x}{x^2} - \tfrac12 + \tfrac{x^2}{24}\big)/x^4 = 0.00138864\) and \(\big(\cosh x - 1 - \tfrac{x^2}{2}\big)/x^4 = 0.04168056\); at \(x = 0.01\), 0.00138888 and 0.04166681. They approach \(1/720 = 0.00138889\) and \(1/24 = 0.04166667\), the next coefficients of the two series. At \(\delta = 0.1\) the ratio \((\cosh\delta - 1)/(\delta^2/2)\) is 1.00083361, against \(1 + \delta^2/12 = 1.00083333\).

5.5 The remainder

A Taylor polynomial is useful only with a bound on its error. The remainder \(R_n(x) = f(x) - P_n(x)\) has an exact formula with one unknown in it.

Theorem (Taylor, Lagrange form of the remainder). If \(f\) has \(n+1\) derivatives on an interval containing 0 and \(x\), then

\[ f(x) = P_n(x) + \frac{f^{(n+1)}(c)}{(n+1)!}\, x^{n+1} \]

for some \(c\) between 0 and \(x\) [spivak], [openstax].

The error looks exactly like the next term of the series, except that the derivative is taken at an unknown point \(c\) instead of at 0. For \(n = 0\) it reads \(f(x) = f(0) + f'(c)\,x\): the mean value theorem (section 2.8), of which this is the generalization. We never learn \(c\); we bound \(f^{(n+1)}\) over the whole interval, and that bounds the error.

Example: \(e^1\). Every derivative of \(e^x\) is \(e^x\), and \(0 < c < 1\) gives \(1 < e^c < e\). So

\[ \frac{1}{(n+1)!} < e - P_n(1) < \frac{e}{(n+1)!}. \]
\(n\)\(1/(n+1)!\)\(e - P_n(1)\)\(e/(n+1)!\)
2\(1.67 \times 10^{-1}\)\(2.18 \times 10^{-1}\)\(4.53 \times 10^{-1}\)
6\(1.98 \times 10^{-4}\)\(2.26 \times 10^{-4}\)\(5.39 \times 10^{-4}\)
10\(2.51 \times 10^{-8}\)\(2.73 \times 10^{-8}\)\(6.81 \times 10^{-8}\)
12\(1.61 \times 10^{-10}\)\(1.73 \times 10^{-10}\)\(4.37 \times 10^{-10}\)

The actual error sits between the two bounds, as promised. The same reasoning answers a design question: on \(|r| \le \tfrac12 \ln 2\) (the interval section 5.6 reduces to), how many terms of the exponential series give a relative error below \(2^{-53}\)? For \(r > 0\), \(e^c / e^r \le 1\); for \(r < 0\), \(e^c \le 1\) and \(e^r \ge e^{-|r|}\). Either way the relative error is at most \(e^{|r|} |r|^{n+1}/(n+1)! \le \sqrt{2}\, |r|^{n+1}/(n+1)!\). That bound is \(2.4 \times 10^{-16}\) at degree 12 and \(5.9 \times 10^{-18}\) at degree 13: degree 13 is enough.

Example: cosh. With \(n = 1\): \(\cosh \delta = 1 + 0 \cdot \delta + \cosh(c)\, \delta^2 / 2\), with \(|c| \le |\delta|\). Since \(1 \le \cosh c \le \cosh \delta\),

\[ 1 + \frac{\delta^2}{2} \;\le\; \cosh \delta \;\le\; 1 + \frac{\delta^2}{2} \cosh \delta. \]

Two-sided bounds like this one, with the function itself on the right, are the kind the paper uses (section 4.6, section 19.4).

For \(\sin\) and \(\cos\) every derivative is bounded by 1, so \(|R_n(x)| \le |x|^{n+1}/(n+1)!\) for all \(x\). With \(n = 3\) for cosine (whose \(P_3 = P_2\)): \(|\cos x - (1 - x^2/2)| \le x^4/24\).

Watch out. The remainder theorem bounds the error of the polynomial in exact arithmetic. Evaluated in doubles, each operation adds its own rounding (section 14.3). A library makes the remainder smaller than the rounding error and then works on the rounding. And Taylor series can fail to converge to \(f\) even where they converge at all; for every function in the table above they do [spivak].

5.6 How a libm approximates

A libm exp is accurate to within about an ulp over the full range of doubles. The Taylor series alone could not do that: at \(x = 700\) it needs over 900 terms, and at \(x = -20\) it cancels to garbage (section 5.3). Section 4.7 shows how a libm exp is assembled: range reduction \(x = k \ln 2 + r\) with \(|r| \le \tfrac12 \ln 2\), a polynomial for \(e^r\), and scaling by \(2^k\). The same shape serves every elementary function: an identity moves the argument into a short interval (for sin and cos, reduction modulo \(\pi/2\), which for huge arguments needs many hundreds of bits of \(2/\pi\)), a polynomial does the work there, and the identity is undone [muller]. This section is about the middle step: which polynomial, and why.

Horner's rule

Evaluate \(c_0 + c_1 r + \dots + c_n r^n\) from the inside out:

\[ c_0 + r\,(c_1 + r\,(c_2 + \dots + r\,(c_{n-1} + r\, c_n))). \]

That is \(n\) multiply-adds and no powers. Each step can be one fused multiply-add, which computes \(p r + c_k\) with a single rounding (section 14.6). Each step also depends on the previous one, so on a pipelined FPU Horner runs at the latency of an fma per coefficient, not at its throughput; libraries split the polynomial into independent pieces (exercise 6) to fill the pipeline. Either way the cost is set by the degree, so the question is how low a degree can reach the target accuracy, \(2^{-53}\) relative on \(|r| \le 0.347\). Section 5.5 showed that the Taylor polynomial needs degree 13.

Why not Taylor

The Taylor polynomial is perfect at 0 and worst at the ends of the interval: its error is about \(r^{n+1}/(n+1)!\), nearly all of it near \(|r| = 0.347\). It spends its accuracy where nothing needs it. A polynomial that spreads the error over the interval can have the same degree and a much smaller maximum error.

One way to get it is interpolation: take the polynomial of degree \(n\) that equals \(f\) at \(n+1\) chosen points \(r_0, \dots, r_n\). Its error is

\[ f(r) - p(r) = \frac{f^{(n+1)}(\xi)}{(n+1)!} \prod_{j=0}^{n} (r - r_j) \]

for some \(\xi\) in the interval [burden]. The first factor is the same as in the Lagrange remainder; the points only enter through the product. The Taylor polynomial is the limit with all points at 0, where the product is \(r^{n+1}\), as large as \(A^{n+1}\) on \([-A, A]\).

The best choice of points makes the largest value of the product as small as possible. The answer is the Chebyshev points, the roots of the Chebyshev polynomial \(T_{n+1}\), defined by \(T_m(\cos\theta) = \cos m\theta\): on \([-A, A]\) they are \(r_j = A \cos \theta_j\) with \(\theta_j = \pi (j + \tfrac12)/(n+1)\). Equal steps in angle, so the points crowd toward the ends, where Taylor's error lives. With them the product is \(A^{n+1}\, T_{n+1}(r/A)/2^n\) (\(T_{n+1}\) has leading coefficient \(2^n\)), and since \(|T_{n+1}| \le 1\) its largest value is \(A^{n+1}/2^n\) [trefethen]. Against Taylor's \(A^{n+1}\), the error bound drops by a factor of \(2^n\).

That is a prediction about real numbers; the program checks it in doubles:

#include <math.h>
#include <stdio.h>

#define A 0.34657359027997264L   /* ln2/2: exp(r) is needed on [-A, A] */

static double horner(const double *c, int n, double r)
{
    double p = c[n];
    for (int k = n - 1; k >= 0; k--)
        p = fma(p, r, c[k]);
    return p;
}

/* The degree-n polynomial equal to exp at the n+1 Chebyshev points of [-A, A],
   in powers of r. Long double keeps the fit's own rounding below 2^-53. */
static void cheb_fit(int n, double *c)
{
    long double t[16][16] = { { 0 } }, a[16], pi = 3.14159265358979323846L;
    t[0][0] = 1; t[1][1] = 1;                 /* t[k][i]: coefficient of u^i in T_k(u) */
    for (int k = 2; k <= n; k++)              /* T_k = 2u T_(k-1) - T_(k-2) */
        for (int i = 0; i <= k; i++)
            t[k][i] = (i ? 2 * t[k - 1][i - 1] : 0) - t[k - 2][i];
    for (int k = 0; k <= n; k++) {            /* a_k = 2/(n+1) sum_j f(u_j) T_k(u_j) */
        a[k] = 0;
        for (int j = 0; j <= n; j++) {
            long double th = pi * (j + 0.5L) / (n + 1);
            a[k] += expl(A * cosl(th)) * cosl(k * th) * 2 / (n + 1);
        }
    }
    a[0] /= 2;
    for (int i = 0; i <= n; i++) {            /* sum_k a_k T_k(r/A), in powers of r */
        long double ci = 0;
        for (int k = i; k <= n; k++) ci += a[k] * t[k][i];
        c[i] = (double)(ci / powl(A, i));
    }
}

static double max_rel_err(const double *c, int n)
{
    double worst = 0;
    for (int i = 0; i <= 100000; i++) {
        double r = (double)(-A + 2 * A * i / 100000);
        double e = (double)fabsl((horner(c, n, r) - expl(r)) / expl(r));
        if (e > worst) worst = e;
    }
    return worst;
}

int main(void)
{
    double tay[16] = { 1 }, ch[16];
    printf("degree  Taylor      Chebyshev   ratio\n");
    for (int n = 3; n <= 13; n++) {
        for (int k = 1; k <= n; k++) tay[k] = tay[k - 1] / k;   /* 1/k! */
        cheb_fit(n, ch);
        double et = max_rel_err(tay, n), ec = max_rel_err(ch, n);
        printf("%4d   %.2e    %.2e    %5.1f\n", n, et, ec, et / ec);
    }
    return 0;
}
degree  Taylor      Chebyshev   ratio
   3   7.94e-04    9.97e-05      8.0
   4   5.57e-05    3.49e-06     15.9
   5   3.24e-06    1.02e-07     31.9
   6   1.61e-07    2.53e-09     63.8
   7   7.03e-09    5.51e-11    127.7
   8   2.72e-10    1.06e-12    255.4
   9   9.44e-12    1.84e-14    512.5
  10   2.98e-13    4.25e-16    701.9
  11   8.69e-15    1.17e-16     74.5
  12   3.24e-16    1.13e-16      2.9
  13   1.12e-16    1.12e-16      1.0

Printed on FreeBSD 15.1 on amd64 (Core i5-9400), clang 19. It depends on the machine: elsewhere the timings, the random numbers or the last digits differ.

How the program carries out the construction:

The ratio is \(2^n\), as derived (8, 16, 32, …, 512), until the errors reach the rounding floor \(2^{-53} \approx 1.1 \times 10^{-16}\): the Chebyshev polynomial gets there at degree 11, Taylor at 13. Two fewer fmas on the critical path of every exp. The coefficients differ from \(1/k!\) only slightly, most in the highest powers, which make up for the terms left off: at degree 5 the coefficient of \(r^4\) is 0.0419175 instead of \(1/24 = 0.0416667\), and that of \(r^5\) is 0.0083691 instead of \(1/120 = 0.0083333\).

What minimax adds

The best polynomial of degree \(n\), the one with the smallest maximum error on the interval, is the minimax polynomial. It is characterized by its error curve: the error reaches its maximum size at \(n+2\) points, with alternating signs (equioscillation). The Chebyshev interpolant nearly has this shape already, because its error is dominated by the \(T_{n+1}\) term, which itself equioscillates; so minimax improves on it only by a small factor, which the Remez algorithm finds by moving the points until the peaks are equal [trefethen]. A libm takes that last step because it is free at run time, and it fits relative rather than absolute error, with the coefficients rounded to doubles as part of the fit [muller]. The bigger gain is in the interval: Tang's method reduces with a 32-entry table of \(2^{j/32}\) to \(|r| \le \ln 2/64\) (section 4.7), where the factor \(A^{n+1}\) is so small that about degree 5 suffices [tang-exp]. A lookup table plus a short polynomial: the same trade of memory for latency as anywhere else.

Exercises

  1. Sum \(1 + \tfrac13 + \tfrac19 + \tfrac1{27} + \dots\), and \(0.1 + 0.01 + 0.001 + \dots\).
    Answer

    Geometric with \(x = 1/3\): \(1/(1 - 1/3) = 3/2\). The second is \(0.1 \cdot 1/(1 - 0.1) = 1/9\), which is \(0.111\ldots\)

  2. Write the Taylor polynomial of degree 6 of \(e^{-x^2}\) at 0 without differentiating.
    Answer

    Put \(-x^2\) for \(x\) in the exponential series: \(1 - x^2 + x^4/2 - x^6/6\). A power series can be substituted into, inside its radius.

  3. Bound the error of \(\sin x \approx x\) for \(|x| \le 0.1\).
    Answer

    \(P_1 = P_2 = x\) for sine, so use \(n = 2\): \(|R_2| \le |x|^3/3! \le 0.001/6 \approx 1.7 \times 10^{-4}\). The true worst error, \(0.1 - \sin 0.1 \approx 1.67 \times 10^{-4}\), is close to it.

  4. How many terms of the exponential series give \(e^{0.5}\) to within \(10^{-10}\)? Use the Lagrange bound, then check with a program.
    Answer

    The bound is \(e^{0.5}\, 0.5^{n+1}/(n+1)!\). It is \(4.4 \times 10^{-10}\) at \(n = 9\) and \(2.0 \times 10^{-11}\) at \(n = 10\): degree 10, eleven terms. terms.c prints an actual error of \(1.3 \times 10^{-11}\).

  5. In the paper, a cluster with logit spread \(\delta_C = 0.2\) contributes \(\cosh \delta_C - 1\). How close is \(\delta_C^2/2\), and in which direction?
    Answer

    \(\cosh 0.2 - 1 = 0.02 (1 + 0.04/12 + \dots) \approx 0.020067\). \(\delta^2/2 = 0.02\) is below it by about 0.33%: \(\cosh \delta - 1 \ge \delta^2/2\) always, since every term of the series is positive.

  6. Horner's rule for \(c_0 + c_1 x + c_2 x^2 + c_3 x^3\) is a chain of three dependent fmas. Rewrite it as \((c_0 + c_1 x) + x^2 (c_2 + c_3 x)\) (Estrin's scheme). Why can it be faster, if the operation count is the same or larger?
    Answer

    \(c_0 + c_1 x\), \(c_2 + c_3 x\) and \(x^2\) do not depend on each other: they issue together, in different pipeline slots. Then one fma finishes: two fma latencies on the critical path instead of three. Latency, not operation count, bounds a dependent chain; for degree 13 the difference is large.

  7. Explain why the series for \(e^{-20}\) loses all its digits, and give two ways to compute \(e^{-20}\) with it accurately.
    Answer

    The terms alternate and the largest is \(\approx 4.3 \times 10^7\); each carries a rounding error near \(10^{-16}\) of its size, about \(4 \times 10^{-9}\), more than the answer \(2.1 \times 10^{-9}\). Fixes: \(1/e^{20}\), whose series has only positive terms (terms.c prints \(2.061154 \times 10^{-9}\), correct), or range reduction: \(e^{-20} = 2^{-29} e^{r}\) with \(r = -20 + 29 \ln 2 \approx 0.101\).

  8. Show from the geometric series that \(\dfrac{1}{(1-x)^2} = 1 + 2x + 3x^2 + 4x^3 + \dots\) for \(|x| < 1\).
    Answer

    Differentiate \(1/(1-x) = \sum x^n\) term by term (allowed inside the radius): \(1/(1-x)^2 = \sum n x^{n-1}\).

References