Calculus is about what happens when a quantity gets close to something: a slope measured over a shrinking interval, a sum over thinner and thinner slices. The tool for “close” is the limit. This chapter sets up the numbers it works on, the functions it is applied to, and the limit itself. Everything later rests on it: derivatives (chapter 2) and integrals (chapter 3) are limits, and the big-O notation of section 1.6 is how the rest of the book says how large an error is.
The real numbers \(\mathbb{R}\) are the points of a line with no gaps. They contain the integers, the fractions (the rationals \(\mathbb{Q}\)), and numbers like \(\sqrt{2}\) and \(\pi\) that are not fractions. Between any two reals there is another one, and another between those, forever.
A double is not a real. It is one of about \(2^{64}\) values, spaced unevenly: dense near zero, sparse far out. The format stores 53 significant bits and an exponent, so a double \(x\) with \(2^e \le x < 2^{e+1}\) is followed by \(x + 2^{e-52}\): the gap is proportional to the size.
| \(x\) | next double after \(x\) | gap |
|---|---|---|
| 1 | 1.0000000000000002 | \(2^{-52} \approx 2.2 \times 10^{-16}\) |
| \(10^6\) | 1000000.0000000001 | \(2^{-33} \approx 1.2 \times 10^{-10}\) |
| \(10^{16}\) | 10000000000000002 | 2 |
Two consequences. Past \(2^{53}\) the gap exceeds 1, so \(2^{53} + 1\) rounds back to \(2^{53}\). And most decimal fractions are not doubles: 0.1, 0.2 and 0.3 are each replaced by the nearest double, so 0.1 + 0.2 gives 0.30000000000000004, the double just above the one nearest 0.3.
Mathematics in this book is done in \(\mathbb{R}\), where \(0.1 + 0.2 = 0.3\) exactly and \(x + 1 \ne x\) always. Programs run on floats, where neither holds. Most of the time the difference is a relative error near \(2^{-53} \approx 1.1 \times 10^{-16}\) per operation and can be ignored. The places where it cannot are pointed out in boxes like this one, and chapter 14 treats floats properly.
In the machine. The gap after 1, \(2^{-52} \approx 2.2 \times 10^{-16}\), is called machine epsilon (DBL_EPSILON in <float.h>). For float it is \(2^{-23} \approx 1.2 \times 10^{-7}\). The x87 unit computes with 64 significant bits internally, which is why the same C code could print different digits on a Pentium Pro and on SSE2 [goldberg].
A function \(f : A \to B\) assigns to each input in the set \(A\) (the domain) exactly one output in \(B\) (the codomain). In C terms it is a pure function B f(A x): no state, no side effects, same input, same output. \(f(x) = x^2\) on \(\mathbb{R}\) is double f(double x) { return x * x; }.
The graph of \(f : \mathbb{R} \to \mathbb{R}\) is the set of points \((x, f(x))\) in the plane: the curve you would plot. Most of calculus is reading things off that curve: its slope, the area under it, where it is highest.
Functions combine as programs do:
if; the function is still one function.A function of several inputs, \(f : \mathbb{R}^d \to \mathbb{R}\), takes an array of \(d\) numbers. The dot product \(q^\top k\) of a query and a key is one. Chapter 7 does calculus on them; until then, one input.
Take \(f(x) = \dfrac{x^2 - 1}{x - 1}\). At \(x = 1\) it is \(0/0\): undefined, a crash. Everywhere else, \(x^2 - 1 = (x-1)(x+1)\), so \(f(x) = x + 1\). Near 1, \(f\) is near 2: \(f(1.001) = 2.001\), \(f(0.999) = 1.999\). We say the limit of \(f\) at 1 is 2:
\[ \lim_{x \to 1} \frac{x^2 - 1}{x - 1} = 2. \]The limit does not care about the value at the point, only about values near it. That is what makes it useful: the slope of a curve at a point is a \(0/0\) (no run, no rise), but the slope over a small interval has a limit as the interval shrinks. That limit is the derivative.
“Near” needs a definition a machine could check. Think of it as a contract. You name a tolerance \(\varepsilon > 0\) on the output. I must answer with a tolerance \(\delta > 0\) on the input, such that every input within \(\delta\) of \(a\) (other than \(a\) itself) gives an output within \(\varepsilon\) of \(L\). If I can always answer, whatever \(\varepsilon\) you pick, then \(L\) is the limit.
Definition. \(\lim_{x \to a} f(x) = L\) means: for every \(\varepsilon > 0\) there is a \(\delta > 0\) such that \(0 < |x - a| < \delta\) implies \(|f(x) - L| < \varepsilon\).
For \(f(x) = x + 1\) near 1, the answer is \(\delta = \varepsilon\): if \(|x - 1| < \varepsilon\) then \(|f(x) - 2| = |x - 1| < \varepsilon\). For curvier functions \(\delta\) is smaller than \(\varepsilon\) and depends on how steep \(f\) is. You will rarely write such arguments; the paper never does. What matters is the idea: a limit is a promise about every tolerance at once, not about any one computed value.
Limits obey the rules you would hope for [openstax]: if \(f \to L\) and \(g \to M\), then \(f + g \to L + M\), \(f g \to L M\), and \(f / g \to L / M\) when \(M \ne 0\). A one-sided limit \(\lim_{x \to a^+}\) only looks at \(x > a\). A limit at infinity \(\lim_{x \to \infty} f(x) = L\) means \(f(x)\) is within \(\varepsilon\) of \(L\) for all \(x\) beyond some point; \(\lim_{x \to \infty} 1/x = 0\).
\(\lim_{x \to 0} \dfrac{1 - \cos x}{x^2} = \dfrac{1}{2}\) (chapter 5 shows why). The limit is a statement about reals. Evaluated in doubles at smaller and smaller \(x\), the same expression first drifts, then gives 0. The identity \(1 - \cos x = 2 \sin^2(x/2)\) gives an equal expression with no subtraction, and that one stays right. Here both are evaluated at \(x = 10^{-1}, \dots, 10^{-9}\):
#include <math.h>
#include <stdio.h>
int main(void)
{
for (int k = 1; k <= 9; k++) {
double x = pow(10, -k);
double naive = (1 - cos(x)) / (x * x);
double s = sin(x / 2);
double stable = 2 * s * s / (x * x); /* 1 - cos x = 2 sin^2(x/2) */
printf("x = 1e-%d naive %.10f stable %.10f\n", k, naive, stable);
}
return 0;
}
x = 1e-1 naive 0.4995834722 stable 0.4995834722 x = 1e-2 naive 0.4999958333 stable 0.4999958333 x = 1e-3 naive 0.4999999583 stable 0.4999999583 x = 1e-4 naive 0.4999999970 stable 0.4999999996 x = 1e-5 naive 0.5000000414 stable 0.5000000000 x = 1e-6 naive 0.5000444503 stable 0.5000000000 x = 1e-7 naive 0.4996003611 stable 0.5000000000 x = 1e-8 naive 0.0000000000 stable 0.5000000000 x = 1e-9 naive 0.0000000000 stable 0.5000000000
The program walks \(x = 10^{-k}\) toward 0, the way the limit does, and evaluates two expressions that are equal as functions of a real \(x\): naive is \(\dfrac{1 - \cos x}{x^2}\), and stable is \(\dfrac{2 \sin^2(x/2)}{x^2}\), with \(s = \sin(x/2)\) computed once and squared. Equal in \(\mathbb{R}\), they differ in doubles because of where the rounding happens. In naive, \(\cos x\) is rounded to the nearest double near 1, where the gap is \(1.1 \times 10^{-16}\), and then the subtraction from 1 throws away everything but that rounding error. In stable, every operation works on numbers of size about \(x\), each rounded with relative error \(10^{-16}\), and the error stays relative. The division by \(x^2\) is the same in both: it magnifies whatever error the numerator has, by \(10^{18}\) at the end.
In the machine. \(\cos(10^{-8})\) is \(1 - 5 \times 10^{-17}\), closer to 1 than the gap below 1 (\(1.1 \times 10^{-16}\)), so it rounds to exactly 1 and the numerator is 0. Subtracting two nearly equal numbers keeps their error and loses their digits: cancellation (section 14.4). The cure is algebra, done before the code runs.
\(f\) is continuous at \(a\) when \(\lim_{x \to a} f(x) = f(a)\): the limit exists, the value exists, and they agree. No hole, no jump. A function continuous at every point of an interval can be drawn there without lifting the pen. Polynomials, \(\exp\), \(\sin\), \(\cos\) are continuous everywhere; sums, products and compositions of continuous functions are continuous; a quotient is continuous wherever its denominator is not zero.
What fails: the step function (0 for \(x < 0\), 1 for \(x \ge 0\)) jumps at 0; \(1/x\) is not defined at 0 and runs off to \(\pm\infty\) beside it.
Theorem (intermediate value). If \(f\) is continuous on \([a, b]\) and \(f(a)\) and \(f(b)\) have opposite signs, then \(f(c) = 0\) for some \(c\) between \(a\) and \(b\) [spivak].
A continuous curve that starts below the axis and ends above it must cross it. This is the correctness proof of bisection. Start from \([a, b]\) with \(f(a)\) and \(f(b)\) of opposite signs. Evaluate \(f\) at the midpoint \(m = (a + b)/2\) and keep whichever half, \([a, m]\) or \([m, b]\), still has ends of opposite signs. The theorem applies again to that half, so a root stays inside at every step, and the width after \(n\) steps is \((b - a)/2^n\): one bit of the answer per evaluation, whatever \(f\) is. For \(f(x) = \cos x - x\) on \([0, 1]\) (\(f(0) = 1\), \(f(1) \approx -0.46\)):
| steps | interval |
|---|---|
| 1 | [0.5, 1] |
| 10 | [0.73828125, 0.7392578125] |
| 20 | [0.73908424377441406, 0.73908519744873047] |
| 50 | [0.73908513321516001, 0.73908513321516089] |
The root is 0.739085133215160…
In the machine. Bisection in doubles stops by itself: once \(a\) and \(b\) are adjacent doubles, \((a + b)/2\) rounds to one of them and the interval cannot shrink. Near 0.74 that happens around step 53, at width \(2^{-53}\). So stop when the midpoint equals an end, rather than at a width you guessed: a guessed width below the gap never comes.
Ten steps gain about three decimal digits (\(2^{10} \approx 10^3\)), so 53 steps exhaust a double. Newton's method (section 2.6) does the same job in five or six steps by using the slope.
A sequence is a function of the integers: \(a_1, a_2, a_3, \dots\), or a[n] for an unbounded array. It converges to \(L\), written \(a_n \to L\) or \(\lim_{n \to \infty} a_n = L\), if for every \(\varepsilon > 0\) the terms are within \(\varepsilon\) of \(L\) from some index on. It is the same contract as before, with “from some index on” in place of “within \(\delta\)”.
Two sequences recur in this book. The first defines the number \(e\):
\[ e = \lim_{n \to \infty} \left(1 + \frac{1}{n}\right)^n \approx 2.718281828. \]Read it as compound interest: 100% interest a year, paid in \(n\) installments, each reinvested. More installments earn more, but the total levels off at \(e\). Chapter 4 builds \(\exp\) from it.
The second is an iteration \(a_{n+1} = g(a_n)\): a loop that refines a guess. Heron's rule \(a_{n+1} = \tfrac{1}{2}(a_n + 2/a_n)\) converges to \(\sqrt{2}\), because if \(a\) is too big then \(2/a\) is too small, and their mean is closer. Both sequences, computed in doubles:
| \(n\) | \((1 + 1/n)^n\) | \(n\) | \(a_n\) (from \(a_0 = 1\)) | \(a_n - \sqrt{2}\) | |
|---|---|---|---|---|---|
| 10 | 2.5937424601 | 1 | 1.5 | \(8.6 \times 10^{-2}\) | |
| \(10^3\) | 2.7169239322 | 2 | 1.4166666666666665 | \(2.5 \times 10^{-3}\) | |
| \(10^5\) | 2.7182682372 | 3 | 1.4142156862745097 | \(2.1 \times 10^{-6}\) | |
| \(10^7\) | 2.7182816941 | 4 | 1.4142135623746899 | \(1.6 \times 10^{-12}\) | |
| \(e\) | 2.7182818285 | 5 | 1.4142135623730949 | \(\approx 10^{-16}\) |
The two converge at very different speeds. The error of \((1 + 1/n)^n\) shrinks like \(1/n\): ten times the work, one more digit. Heron's error is squared each step (\(10^{-3}, 10^{-6}, 10^{-12}\)): the digits double. That is quadratic convergence, and it is Newton's method in disguise (section 2.6).
Fact (monotone convergence). A sequence that only increases and never exceeds some bound converges [spivak]. This is the property that \(\mathbb{R}\) has “no gaps”: in \(\mathbb{Q}\), Heron's sequence decreases toward \(\sqrt{2}\) with nothing to land on.
You know big-O from algorithms: quicksort is \(O(n \log n)\) on average. The definition is the same in calculus, but it is used two ways: as \(n \to \infty\) (cost grows) and as \(x \to 0\) (error shrinks).
Definition. \(f(x) = O(g(x))\) as \(x \to a\) means there is a constant \(M\) with \(|f(x)| \le M |g(x)|\) for all \(x\) close enough to \(a\). \(f(x) = o(g(x))\) means \(f(x) / g(x) \to 0\): \(f\) is negligible next to \(g\).
As \(x \to 0\), higher powers are smaller: \(x^3 = O(x^2)\), and in fact \(x^3 = o(x^2)\). So \(O(x^2)\) as \(x \to 0\) is better than \(O(x)\): halve \(x\), and an \(O(x^2)\) error drops by 4, an \(O(x)\) error only by 2. That is how numerical methods are ranked: the forward difference has error \(O(h)\), the central difference \(O(h^2)\) (section 2.5); Simpson's rule \(O(h^4)\) (section 3.7).
Written inside a formula, \(O(\cdot)\) stands for “some term no bigger than this”:
\[ \cos x = 1 - \frac{x^2}{2} + O(x^4) \quad (x \to 0). \]This one line explains the limit of section 1.3: \(\dfrac{1 - \cos x}{x^2} = \dfrac{1}{2} + O(x^2) \to \dfrac{1}{2}\). Expansions like it are the subject of chapter 5, and the paper's bounds use the same kind of statement: \(\cosh \delta - 1 \approx \delta^2 / 2\) for small \(\delta\) (section 4.6).
Watch out. \(=\) in \(f = O(g)\) is one-way: \(x^2 = O(x)\) as \(x \to 0\), but not \(O(x) = x^2\). Read it as “is in the class”. And the constant \(M\) is hidden: an \(O(h^4)\) method with a huge \(M\) can lose to an \(O(h^2)\) one at practical \(h\), the same way an \(O(n)\) algorithm with a cache miss per element loses to an \(O(n \log n)\) one that streams.
4, since \(x^2 - 4 = (x - 2)(x + 2)\). And \(\dfrac{(1+x) - 1}{x(\sqrt{1+x} + 1)} = \dfrac{1}{\sqrt{1+x} + 1} \to \dfrac{1}{2}\).
\(|f(x) - 5| = 3|x - 2|\), so \(\delta = \varepsilon / 3\) works. Steeper function, smaller \(\delta\).
(sqrt(1 + x) - 1) / x in doubles, fails for small \(x\). Why, and what form computes it well?
\(\sqrt{1+x}\) and 1 are nearly equal, so the subtraction cancels (section 1.3). The rewritten form 1 / (sqrt(1 + x) + 1) has no subtraction and is accurate for every small \(x\).
Yes. \(|\sin| \le 1\), so \(|f(x)| \le |x| \to 0 = f(0)\). Take \(\delta = \varepsilon\).
The width after \(n\) steps is \(2^{-n}\); \(2^{-20} \approx 9.5 \times 10^{-7}\), so 20 steps. Near 0.74 adjacent doubles are \(2^{-53} \approx 1.1 \times 10^{-16}\) apart, so the interval cannot get narrower than that: the midpoint rounds to an end (section 1.4's box).
For \(|x| \le 1\), \(|x^2 + x^3| \le x^2 + |x|^3 \le 2x^2\), so \(M = 2\). As \(x \to \infty\), \((x^2 + x^3)/x^2 = 1 + x\) is unbounded: no \(M\) works.
\(a_{n+1} - \sqrt{c} = \dfrac{a_n^2 - 2a_n\sqrt{c} + c}{2a_n} = \dfrac{e_n^2}{2a_n}\). The new error is a constant times the old error squared, so the exponent of the error doubles each step. It also shows the error is never negative after the first step (up to rounding): Heron approaches from above.