ML Foundations Lecture 3 of 14
Part I — The Language of Functions · Lecture 3

Limits and Continuity

A limit answers a question the function itself refuses to: what value is this heading towards at a point where it may not even be defined? Every derivative, every integral, and every convergence guarantee in optimisation is built on this one idea.

By the end you can

  • State what \(\lim_{x\to c} f(x) = L\) means, both informally and in \(\varepsilon\)–\(\delta\) form
  • Compute limits by factoring, and explain why \(\tfrac00\) is indeterminate rather than undefined
  • Distinguish 'the limit exists' from 'the function is defined' from 'the function is continuous'
  • Recognise the three ways continuity fails and why each matters computationally
  • See why floating-point arithmetic makes naive numerical differentiation fail

Assumed

  • Lectures 1–2: domain, inverse functions, exponentials and logs
  • Factoring quadratics and simplifying rational expressions

1The question limits answer

Consider

\[f(x) = \frac{x^2-9}{x-3}.\]

By the domain rules of Lecture 1, \(x=3\) is excluded — the denominator vanishes. The function genuinely does not exist there. And yet, everywhere near 3, the function is doing something very definite. Factor the numerator:

\[\frac{x^2-9}{x-3} = \frac{(x-3)(x+3)}{x-3} = x+3 \qquad \textbf{provided } x \neq 3.\]

That proviso is not a technicality — cancelling \((x-3)\) requires dividing by it, which requires it to be nonzero. So \(f\) is the line \(y=x+3\) with a single point punctured out at \(x=3\). As \(x\) approaches 3 from either side, \(f(x)\) approaches 6. It never gets there, because there is nothing there. But 6 is unmistakably the value it is heading for.

Intuition

A limit is a statement about the neighbourhood of a point, deliberately excluding the point itself. "\(\lim_{x\to c}f(x)=L\)" says: I can force \(f(x)\) to be as close to \(L\) as you like, by keeping \(x\) close enough to \(c\) — and I never need to evaluate \(f\) at \(c\) to do it. That last clause is the whole point. It is what lets us define the derivative, which is a ratio that is \(\tfrac00\) at the one point we care about.

A removable hole: f(x) = (x² − 9)/(x − 3)Interactive
f(x)x approaching 3
2.000
Slide x towards 3 from either side and read the output. The value marches towards 6 with no drama. Land exactly on x = 3 and the function reports nothing at all — the open circle. The limit is 6; the value f(3) does not exist. These are different claims, and keeping them apart is the point of this lecture.

2The definition

The informal version — "gets arbitrarily close" — is good intuition and useless for proofs, because "close" is not a number. The fix, due to Cauchy and Weierstrass, replaces both vague words with quantified numbers.

Definition — limit (ε–δ)

Let \(f\) be defined on an open interval around \(c\), except possibly at \(c\) itself. We say

\[\lim_{x\to c} f(x) = L\]

if: for every \(\varepsilon>0\), there exists \(\delta>0\) such that

\[ 0 < |x-c| < \delta \quad\Longrightarrow\quad |f(x)-L| < \varepsilon. \]

Read it as a game between two players. Your opponent names a tolerance \(\varepsilon\) — how close to \(L\) they demand the output be. You must respond with a radius \(\delta\) — how close to \(c\) you promise to keep the input. You win if your promise always delivers. The limit exists iff you have a winning response to every \(\varepsilon\), no matter how small.

Two details that carry the mathematical weight:

The ε–δ game on f(x) = x² at c = 2Interactive
f(x) = x²ε-band around Lδ-band around c
1.00
0.80
You win a round when the whole green vertical strip fits inside the pink horizontal band — that means every x within δ of 2 produces an output within ε of 4. Shrink ε and you will have to shrink δ to keep winning. The limit exists because you can always win: δ = min(1, ε/5) works for every ε, as the algebra below the plot shows.
The algebra behind that guaranteed δ

Why does \(\delta=\min(1,\varepsilon/5)\) always work for \(\lim_{x\to2}x^2=4\)? Try to reconstruct it: you need to bound \(|x^2-4|\) in terms of \(|x-2|\).

We must make \(|x^2-4|<\varepsilon\). Factor:

\[|x^2-4| = |x-2|\,|x+2|.\]

The first factor is what we control. The second is a nuisance — it depends on \(x\). The standard trick is to bound the nuisance first by pre-committing to a modest \(\delta\).

Insist \(\delta\le1\). Then \(|x-2|<1\) gives \(1<x<3\), hence \(3<x+2<5\), hence \(|x+2|<5\). So

\[|x^2-4| = |x-2|\,|x+2| < 5\,|x-2|.\]

Now to force \(|x^2-4|<\varepsilon\) it suffices to force \(5|x-2|<\varepsilon\), i.e. \(|x-2|<\varepsilon/5\).

Both requirements are met by \(\delta=\min(1,\varepsilon/5)\): the \(1\) keeps the bound on \(|x+2|\) valid, and the \(\varepsilon/5\) does the actual work. Given any \(\varepsilon>0\) this \(\delta\) is positive, so we have a winning response to every challenge. ∎

You will not do many of these by hand. The reason to do one is to see that "approaches" is a completely precise notion with an algorithm behind it — not an appeal to intuition. Every convergence theorem in optimisation ("SGD converges at rate \(O(1/\sqrt{T})\)") is a statement of exactly this shape, with \(\varepsilon\) traded for an error bound and \(\delta\) for an iteration count.

3Computing limits, and what 0/0 really means

In practice you almost never use \(\varepsilon\)–\(\delta\). You use limit laws — sums, products, quotients of limits behave as you would hope — plus one strategy for the interesting cases.

When you substitute \(x=c\) and get an honest number, that is the answer (for continuous functions, which is most of them). When you get \(\tfrac00\), you have an indeterminate form. That phrase is precise and worth unpacking.

Common trap

\(\tfrac00\) is not "undefined" in the way \(\tfrac10\) is. \(\tfrac10\) has no possible answer. \(\tfrac00\) has too many: it tells you the limit could be anything, and you must dig further. Compare, all as \(x\to0\):

\[\frac{x}{x}\to 1, \qquad \frac{x^2}{x}\to 0, \qquad \frac{x}{x^2}\to \pm\infty, \qquad \frac{\sin x}{x}\to 1, \qquad \frac{2x}{x}\to 2 .\]

Every one of them is \(\tfrac00\) on substitution, and they have five different answers. The symbol \(\tfrac00\) carries no information; the rate at which each part goes to zero carries all of it.

The standard strategy: cancel the vanishing factor0 / 4
\[\lim_{x\to 3}\frac{x^2-9}{x-3} \;\longrightarrow\; \frac{0}{0}\]
Substitute first. Always try it. Getting a number means you are done; getting \(\tfrac00\) tells you numerator and denominator share a root at \(x=3\), which means they share a factor \((x-3)\).
\[\frac{x^2-9}{x-3} = \frac{(x-3)(x+3)}{x-3}\]
Factor out the shared root. This is guaranteed possible: the factor theorem says if a polynomial \(p\) has \(p(3)=0\) then \((x-3)\) divides \(p\). The \(\tfrac00\) was the signal that both do.
\[= x+3 \qquad \text{for all } x\neq 3\]
Cancel — and record the proviso. Dividing by \((x-3)\) requires \(x\neq3\). But the limit deliberately excludes \(x=3\), so the restriction costs us nothing. This is the exact moment the \(0<|x-c|\) clause in the definition earns its keep.
\[\lim_{x\to3}(x+3) = 6\]
Substitute into the simplified form. \(x+3\) is a polynomial, hence continuous, so its limit equals its value. The original ratio and \(x+3\) agree at every point the limit cares about, so they have the same limit.
Check yourself
Evaluate \(\displaystyle\lim_{h\to0}\frac{(2+h)^2-4}{h}\). (Recognise this shape — it is a derivative in disguise, and you will compute hundreds of them next lecture.)
Correct. Correct. Expand: \((2+h)^2-4 = 4+4h+h^2-4 = 4h+h^2 = h(4+h)\). Divide by \(h\) (legal, since \(h\neq0\) inside the limit): the ratio is \(4+h\). As \(h\to0\) that tends to \(4\).

What you just computed is the slope of \(y=x^2\) at \(x=2\), i.e. \(f'(2)\) where \(f(x)=x^2\). The general formula \(f'(x)=2x\) gives \(f'(2)=4\) ✓. Every derivative is a \(\tfrac00\) limit whose vanishing factor cancels.
Not quite. The numerator does tend to 0 — but so does the denominator, and that is exactly the situation where you cannot read off the answer. \(\tfrac00\) is indeterminate. Cancel the factor of \(h\) first: \(\frac{h(4+h)}{h}=4+h\to 4\).
Not quite. The definition never evaluates at \(h=0\) — the clause \(0<|h-0|\) forbids it. We only ever divide by nonzero \(h\), which is perfectly legal. The whole reason limits were invented was to make sense of exactly this situation.

4Continuity

Now we can say precisely what it means for a function to have no jumps, gaps, or holes.

Definition — continuity at a point

\(f\) is continuous at \(c\) if all three of the following hold:

  1. \(f(c)\) exists — that is, \(c\) is in the domain;
  2. \(\lim_{x\to c}f(x)\) exists;
  3. the two agree: \(\displaystyle\lim_{x\to c}f(x) = f(c)\).

\(f\) is continuous on a set if it is continuous at every point of that set.

Three separate conditions, and each can fail independently. That is not pedantry — it is a complete classification of how continuity breaks:

The three ways continuity failsInteractive
0
In every case look at x = 1 and ask the three questions in order: is there a value there? is there a limit there? do they match? The answers classify the failure.
Where this shows up in ML

Continuity is not decorative in machine learning — it is a hard requirement in one place and a violated-but-tolerated assumption in another.

5Limits in floating point

A limit says: as \(h\) shrinks towards zero, the ratio approaches the answer. Your computer disagrees, and the way it disagrees is instructive.

Numerical differentiation: the U-shaped error curvePython · NumPy
Practical engineering note

This is why nobody computes gradients by finite differences in deep learning. The error is U-shaped in \(h\): shrink \(h\) and truncation error falls but roundoff error rises, and the best you can hope for is roughly \(\sqrt{\varepsilon_{\text{machine}}} \approx 10^{-8}\) relative accuracy — for one partial derivative, at the cost of one extra function evaluation. A network with \(10^{9}\) parameters would need \(10^{9}\) forward passes per gradient. Automatic differentiation, which we build in Phase D, computes all \(10^{9}\) partials exactly (to machine precision) in the cost of about two forward passes. Finite differences survive only as gradcheck: a unit test for your autodiff, run on tiny inputs.

6Paying the debts from Lecture 2

We flagged two promissory notes. Both are limits, and both can now at least be stated precisely.

  1. Defining \(a^x\) for irrational \(x\). We set \(a^x=\lim_{r\to x, r\in\mathbb{Q}}a^r\). Making this rigorous needs the completeness of \(\RR\) (every bounded increasing sequence converges), which is the axiom that distinguishes \(\RR\) from \(\mathbb{Q}\). The payoff is that \(a^x\) is continuous, and continuity is what lets us pass limits through it.
  2. The constant \(C(a)=\lim_{h\to0}\frac{a^h-1}{h}\). This is a \(\tfrac00\) form that does not yield to factoring — there is no algebraic cancellation available. It turns out \(C(a)=\ln a\), which we will prove in Lecture 5 once we have the chain rule. Numerically you saw it in the last lecture's interactive: at \(a=e\), \(C(a)=1\).

I am marking both as genuinely deferred rather than skipped. If you want the completeness argument now, say so and we will do it — it is about a page and it is beautiful. Otherwise it belongs in a real analysis detour after Phase C.

7Exercises

★

Evaluate each limit, showing the algebra. Say which are indeterminate on substitution.

(a) \(\displaystyle\lim_{x\to2}\frac{x^2-4}{x-2}\)    (b) \(\displaystyle\lim_{x\to0}\frac{\sqrt{x+4}-2}{x}\)    (c) \(\displaystyle\lim_{x\to\infty}\frac{3x^2-x}{5x^2+7}\)    (d) \(\displaystyle\lim_{x\to0}\frac{|x|}{x}\)

(a) \(\tfrac00\). Factor: \(\frac{(x-2)(x+2)}{x-2}=x+2\to 4\).

(b) \(\tfrac00\). No obvious factor, so rationalise — multiply by the conjugate:

\[\frac{\sqrt{x+4}-2}{x}\cdot\frac{\sqrt{x+4}+2}{\sqrt{x+4}+2} = \frac{(x+4)-4}{x(\sqrt{x+4}+2)} = \frac{x}{x(\sqrt{x+4}+2)} = \frac{1}{\sqrt{x+4}+2}.\]

Now substitute: \(\frac{1}{2+2}=\frac14\). The conjugate trick is the square-root analogue of factoring — it manufactures the cancelling factor.

(c) Not \(\tfrac00\) but \(\tfrac\infty\infty\), also indeterminate. Divide top and bottom by the highest power \(x^2\): \(\frac{3-1/x}{5+7/x^2}\to\frac{3-0}{5+0}=\frac35\).

(d) \(\tfrac00\). From the right, \(|x|=x\) so the ratio is \(+1\). From the left, \(|x|=-x\) so the ratio is \(-1\). The one-sided limits differ, so the limit does not exist — a jump discontinuity. (This function is \(\operatorname{sign}(x)\), and its non-existent limit at 0 is exactly why \(|x|\) is not differentiable there, which is why ReLU is not differentiable at 0.)

★★

Define \[ f(x) = \begin{cases} \dfrac{x^2-x-6}{x-3}, & x\neq 3\\[6pt] k, & x=3\end{cases}\]

Find the value of \(k\) that makes \(f\) continuous at \(3\), and justify each of the three continuity conditions.

Factor the numerator: \(x^2-x-6=(x-3)(x+2)\). For \(x\neq3\), \(f(x)=x+2\).

Condition 2 (limit exists): \(\lim_{x\to3}f(x)=\lim_{x\to3}(x+2)=5\). ✓ regardless of \(k\), since the limit ignores \(x=3\).

Condition 1 (value exists): \(f(3)=k\), which exists for any real \(k\). ✓

Condition 3 (they agree): requires \(k=5\).

So \(k=5\), and the discontinuity was removable — as it always is when the limit exists but the value is wrong or missing. Note the structure of the answer: we did not "plug in and hope", we checked the three conditions in order and found exactly one of them constrained \(k\).

★★★

Prove from the \(\varepsilon\)–\(\delta\) definition that \(\displaystyle\lim_{x\to4}(3x-5)=7\).

Then explain, in one sentence each, what would go wrong in your proof if the limit were claimed to be \(7.001\) instead.

Scratch work (find \(\delta\) first, then write the proof forwards). We need \(|(3x-5)-7|<\varepsilon\), i.e. \(|3x-12|<\varepsilon\), i.e. \(3|x-4|<\varepsilon\), i.e. \(|x-4|<\varepsilon/3\). So \(\delta=\varepsilon/3\) should work — and here, unusually, the chain is reversible, so no bounding trick is needed.

Proof. Let \(\varepsilon>0\) be given. Choose \(\delta=\varepsilon/3>0\). Suppose \(0<|x-4|<\delta\). Then

\[|(3x-5)-7| = |3x-12| = 3|x-4| < 3\delta = 3\cdot\frac{\varepsilon}{3} = \varepsilon.\]

Since \(\varepsilon>0\) was arbitrary, \(\lim_{x\to4}(3x-5)=7\). ∎

Why \(7.001\) fails. Take the specific challenge \(\varepsilon=0.0005\). We would need \(|(3x-5)-7.001|<0.0005\) whenever \(|x-4|<\delta\). But \((3x-5)-7.001 = 3(x-4)-0.001\), and as \(x\to4\) this tends to \(-0.001\), whose magnitude is \(0.001 > 0.0005\). So for every \(\delta\) there are points \(x\) in the punctured interval where the inequality fails — no \(\delta\) can win that round. In general: the limit, when it exists, is unique, and any wrong candidate is defeated by choosing \(\varepsilon\) smaller than half the gap between it and the true limit.

★★

Coding. The central difference approximates a derivative as \(\frac{f(x+h)-f(x-h)}{2h}\). Modify the numerical-differentiation code above to compare forward and central differences on \(f(x)=e^x\) at \(x=1\).

  1. Which achieves lower minimum error, and by roughly what factor?
  2. At which \(h\) does each bottom out?
  3. Truncation error is \(O(h)\) for forward and \(O(h^2)\) for central. Roundoff is \(O(\varepsilon/h)\) for both. Use that to predict the optimal \(h\) for each, and check it against your numbers.
import numpy as np
f, x, exact = np.exp, 1.0, np.e
for k in range(1, 16):
    h = 10.0**(-k)
    fwd = (f(x+h) - f(x)) / h
    ctr = (f(x+h) - f(x-h)) / (2*h)
    print(f"{h:.0e}  fwd err {abs(fwd-exact):.3e}   ctr err {abs(ctr-exact):.3e}")

1 & 2. Forward bottoms out around \(h\approx10^{-8}\) with error \(\sim10^{-8}\). Central bottoms out around \(h\approx10^{-5}\) to \(10^{-6}\) with error \(\sim10^{-11}\) — roughly a thousandfold improvement, for one extra function evaluation.

3. Predicting the optimum. Total error models:

\[E_{\text{fwd}}(h)\approx \frac{M h}{2}+\frac{2\varepsilon}{h}, \qquad E_{\text{ctr}}(h)\approx \frac{M h^2}{6}+\frac{\varepsilon}{h}.\]

Differentiate each and set to zero. For forward: \(\frac{M}{2}-\frac{2\varepsilon}{h^2}=0 \Rightarrow h^\star = 2\sqrt{\varepsilon/M} \sim \sqrt{\varepsilon}\approx 1.5\times10^{-8}\) ✓.

For central: \(\frac{Mh}{3}-\frac{\varepsilon}{h^2}=0 \Rightarrow h^{\star3}=3\varepsilon/M \Rightarrow h^\star \sim \varepsilon^{1/3}\approx 6\times10^{-6}\) ✓, with resulting error \(\sim\varepsilon^{2/3}\approx 4\times10^{-11}\) ✓.

The cube root beats the square root, which is the whole story. This is why gradcheck implementations universally use central differences — and why, even so, they are only a test and never the training algorithm.


Next: Part II opens with the derivative. You have already computed one — the quiz limit \(\lim_{h\to0}\frac{(2+h)^2-4}{h}=4\) was \(f'(2)\) for \(f(x)=x^2\). Now we do it in general, and the whole of optimisation opens up.