Walk towards a wall, each step covering half the distance that's left. You get closer and closer: 1 metre away, then 50 cm, 25 cm, 12.5 cm... You never actually touch the wall, but everyone can see where you're heading: the wall, at distance 0. That "where you're heading" is a limit.
Limits are what make calculus exact. In what is calculus? you zoomed in on a curve and watched its slope settle towards a number: 2.1, 2.01, 2.001, heading for 2. A limit is the precise name for that number. This lesson explains limits and continuity from that idea: how to find what a function is heading towards with a table of values, what happens at holes, jumps and infinity, what makes a function continuous, and why machine learning prefers smooth functions it can take slopes of.
What you need first: what is calculus?, what is a function? and piecewise and step functions.
Key terms
- Limit: the value a function's output heads towards as its input gets closer and closer to some value. Written lim (x → a) f(x).
- One-sided limit: the value approached from only one direction: from the left (x → a⁻) or from the right (x → a⁺).
- Limit at infinity: the value a function settles towards as x grows without end.
- Continuous: you can draw the graph without lifting your pen: no holes, jumps or breaks.
- Discontinuity: a point where a function isn't continuous.
A function with a hole
Here's a function that can't be calculated at x = 1:
f(x) = (x² − 1) ÷ (x − 1)
At x = 1 it gives 0 ÷ 0, which has no meaning. Python agrees:
import numpy as np
import matplotlib.pyplot as plt
def f(x):
return (x ** 2 - 1) / (x - 1)
try:
print(f(1))
except ZeroDivisionError as error:
print("f(1) fails:", error)
f(1) fails: division by zero
But you can ask where f is heading as x gets close to 1. Make a table, creeping up from both sides:
for x in [0.9, 0.99, 0.999, 1.001, 1.01, 1.1]:
print(f"x = {x:<6} f(x) = {f(x):.4f}")
x = 0.9 f(x) = 1.9000
x = 0.99 f(x) = 1.9900
x = 0.999 f(x) = 1.9990
x = 1.001 f(x) = 2.0010
x = 1.01 f(x) = 2.0100
x = 1.1 f(x) = 2.1000
From the left, f(x) climbs 1.9, 1.99, 1.999; from the right it falls 2.1, 2.01, 2.001. Both sides close in on 2, even though f(1) itself doesn't exist.
Written as a limit: lim (x → 1) (x² − 1) ÷ (x − 1) = 2. Read it as "the limit, as x approaches 1, of f of x, is 2".
Why 2? Algebra shows it. x² − 1 factorises as (x − 1)(x + 1), so for every x except 1, f(x) = (x − 1)(x + 1) ÷ (x − 1) = x + 1. The graph is the straight line y = x + 1 with a single point missing:
x = np.linspace(-1, 3, 400)
x = x[np.abs(x - 1) > 1e-9] # skip x = 1 exactly
fig, ax = plt.subplots(figsize=(7, 4.5))
ax.plot(x, f(x), color="#c2410c", linewidth=2.5, label="f(x) = (x² − 1) / (x − 1)")
ax.plot(1, 2, "o", markersize=10, markerfacecolor="white", markeredgecolor="#c2410c", markeredgewidth=2)
ax.annotate("hole at (1, 2): f(1) doesn't exist,\nbut f(x) heads towards 2", (1, 2), textcoords="offset points", xytext=(15, -35))
ax.set_xlabel("x")
ax.set_ylabel("f(x)")
ax.set_title("The limit is the value the curve heads towards")
ax.legend(loc="upper left")
plt.show()
The key idea: a limit only cares about what happens near the point, not at it. The function doesn't even need to exist at x = 1 for its limit there to be 2.
When there's no limit: jumps
A parking garage charges ₹50 for up to 1 hour and ₹100 for anything over an hour. Approach 1 hour from below and the price is 50; approach from above and it's 100:
def parking_price(hours):
return 50 if hours <= 1 else 100
for hours in [0.9, 0.99, 0.999, 1.001, 1.01, 1.1]:
print(f"{hours:<6} hours: ₹{parking_price(hours)}")
0.9 hours: ₹50
0.99 hours: ₹50
0.999 hours: ₹50
1.001 hours: ₹100
1.01 hours: ₹100
1.1 hours: ₹100
The two sides head for different values. So there are two one-sided limits, from the left (50) and from the right (100), but no single limit. A limit exists only when both sides agree.
Limits at infinity
Sometimes the question is where a function settles as x grows huge. The sigmoid function, used to turn scores into probabilities, is a good example:
σ(x) = 1 ÷ (1 + e⁻ˣ)
def sigmoid(x):
return 1 / (1 + np.exp(-x))
for x in [1, 5, 10, 20, -5, -20]:
print(f"x = {x:4}: sigmoid = {sigmoid(x):.10f}")
x = 1: sigmoid = 0.7310585786
x = 5: sigmoid = 0.9933071491
x = 10: sigmoid = 0.9999546021
x = 20: sigmoid = 0.9999999979
x = -5: sigmoid = 0.0066928509
x = -20: sigmoid = 0.0000000021
As x grows, the sigmoid gets closer and closer to 1 (0.99995 at x = 10, 0.999999998 at x = 20) but never reaches it. As x heads to minus infinity, it approaches 0. So lim (x → ∞) σ(x) = 1 and lim (x → −∞) σ(x) = 0. That's why the sigmoid's output can always be read as a probability: it stays between 0 and 1 however large the score.
Continuity: no lifting your pen
A function is continuous at a point when its graph passes through without a break. More exactly, three things must hold at x = a:
- f(a) exists: there's a point there.
- The limit as x → a exists: both sides head for the same value.
- They're equal: the curve heads exactly to the point that's there.
The three kinds of break match the three conditions:
| Break | Example | Which condition fails |
|---|---|---|
| Hole | (x² − 1) ÷ (x − 1) at x = 1 | 1: the point is missing |
| Jump | The parking price at 1 hour | 2: the two sides disagree |
| Blow-up | 1 ÷ x at x = 0 | 2: the values shoot off to ±infinity |
fig, axes = plt.subplots(1, 3, figsize=(13, 4))
xs = np.linspace(-2, 3, 500)
axes[0].plot(xs, np.where(np.abs(xs - 1) < 0.01, np.nan, xs + 1), color="#c2410c")
axes[0].plot(1, 2, "o", markerfacecolor="white", markeredgecolor="#c2410c", markersize=9)
axes[0].set_title("Hole")
axes[1].plot(xs[xs <= 1], np.full((xs <= 1).sum(), 50), color="#c2410c")
axes[1].plot(xs[xs > 1], np.full((xs > 1).sum(), 100), color="#c2410c")
axes[1].set_ylim(0, 130)
axes[1].set_title("Jump")
left, right = xs[xs < -0.05], xs[xs > 0.05]
axes[2].plot(left, 1 / left, color="#c2410c")
axes[2].plot(right, 1 / right, color="#c2410c")
axes[2].set_ylim(-10, 10)
axes[2].set_title("Blow-up: 1 / x")
for ax in axes:
ax.axhline(0, color="grey", linewidth=0.6)
ax.set_xlabel("x")
plt.suptitle("Three ways a function can break")
plt.tight_layout()
plt.show()
Almost every function you'll meet in machine learning, such as polynomials, eˣ, the sigmoid and logarithms (for positive x), is continuous everywhere it's defined.
Why machine learning cares
Derivatives are limits. The slope at a point is the limit of rise ÷ run as the run shrinks to 0. A limit is what turns "2.1, 2.01, 2.001..." into exactly 2. The next lessons build on this.
Learning needs smooth functions. A model learns by nudging its weights and watching the error change. If the error jumps, a tiny nudge can cause a huge change or none at all, and there's no slope to follow. That's why:
- Early neural networks used a step function (output 0 or 1, a jump) and couldn't be trained with gradients. Replacing it with the smooth sigmoid made gradient-based training possible.
- Classifiers are trained on log loss rather than accuracy: accuracy jumps from one value to another as predictions flip, but log loss changes smoothly; see loss functions explained.
- ReLU, max(0, x), the most popular activation in deep learning, is continuous but has a sharp corner at 0. It works because the corner is only at one point, and libraries simply pick a slope there.
Computers and "very close"
Computers can't approach a limit forever, because they store numbers with about 16 significant digits. Try the slope of y = x² at x = 1 from the last lesson, rise ÷ run, with smaller and smaller steps. The true limit is 2:
for step in [1e-4, 1e-8, 1e-12, 1e-15]:
slope = ((1 + step) ** 2 - 1 ** 2) / step
print(f"step {step:.0e}: slope = {slope}")
step 1e-04: slope = 2.000099999999172
step 1e-08: slope = 1.999999987845058
step 1e-12: slope = 2.000177801164682
step 1e-15: slope = 2.220446049250313
With a step of 10⁻⁴ the slope is 2.0001, close to 2. At 10⁻⁸ it's very accurate. But at 10⁻¹² the error grows again (2.00018), and at 10⁻¹⁵ the answer is 2.22, which is plainly wrong: (1 + 10⁻¹⁵)² and 1 differ only in the last few stored digits, so the subtraction loses almost all its accuracy. In exact maths, smaller is always better; on a computer, there's a sweet spot.
Numerical derivatives with Python shows how to choose a step size that's small enough but not too small.
Common mistakes
- Thinking the limit is just f(a). It's what f heads towards, which can exist when f(a) doesn't (a hole), or differ from it.
- Checking only one side. A limit exists only if the left and right limits are the same.
- Writing 0 ÷ 0 = 1 (or 0). 0 ÷ 0 is undefined; it's a signal to simplify or to look at the limit.
- Trusting very tiny steps on a computer. Steps below about 10⁻⁸ can give rounding garbage.
Practice
- Use a table of values to find lim (x → 2) (x² − 4) ÷ (x − 2). Then explain it with algebra.
Show the answer
g = lambda x: (x ** 2 - 4) / (x - 2)
print([round(g(x), 4) for x in [1.99, 1.999, 2.001, 2.01]])
[3.99, 3.999, 4.001, 4.01]
The values head for 4. Algebra: x² − 4 = (x − 2)(x + 2), so g(x) = x + 2 for x ≠ 2, which approaches 2 + 2 = 4.
- Is f(x) = |x| (the absolute value) continuous at 0? Does it have a slope there?
Show the answer
It's continuous: both sides head for 0 and |0| = 0, so you can draw it without lifting your pen. But it has a sharp corner: the slope is −1 just left of 0 and +1 just right, so there's no single slope at 0. Continuous doesn't guarantee smooth.
- What does 1 ÷ x approach as x → infinity? As x → 0 from the right?
Show the answer
As x → infinity, 1 ÷ x → 0 (1 ÷ 1,000,000 = 0.000001). As x → 0 from the right, 1 ÷ x grows without bound: there's no finite limit; we say it tends to infinity.
- Why can't a neural network that uses a step function (0 for negative inputs, 1 otherwise) be trained with gradient descent?
Show the answer
The step function is flat everywhere except at 0, where it jumps. Its slope is 0 almost everywhere and undefined at the jump, so the gradient gives no information about which way to change the weights. A smooth function such as the sigmoid has a useful slope everywhere.
Interview questions
Why must activation and loss functions be differentiable (or nearly so) in neural networks?
Training uses gradient descent with backpropagation, which needs the derivative of the loss with respect to every weight. Discontinuous functions such as step functions have zero or undefined derivatives, so no learning signal flows. Functions with only a few corners, such as ReLU, are fine in practice because a subgradient can be used at the corner.
Why do we train classifiers on log loss instead of accuracy?
Accuracy is a step function of the weights: it only changes when a prediction flips, so its gradient is zero almost everywhere. Log loss changes smoothly with the predicted probabilities, so its gradient tells the optimiser how to improve even when no predictions flip.
Key takeaways
- A limit is the value a function heads towards as its input approaches a point; it ignores the point itself.
- Find limits with a table of values from both sides, or by simplifying the algebra.
- A limit exists only if the left and right sides agree; jumps and blow-ups have no limit.
- A continuous function has no holes, jumps or blow-ups: f(a) exists and equals the limit.
- Derivatives are limits, and machine learning prefers smooth functions because it learns by following slopes.
Frequently asked questions
Do I need the formal epsilon-delta definition of a limit?
Not for machine learning. The formal definition makes "gets closer and closer" mathematically precise, and it's essential for proofs. For understanding derivatives and training models, the intuitive idea and a table of values are enough.
Is every continuous function differentiable?
No. |x| and ReLU are continuous but have corners where there's no single slope. Every differentiable function is continuous, but not the other way round.
What does "the limit doesn't exist" mean in practice?
The function doesn't settle on one value: the two sides disagree (a jump), the values grow without bound (a blow-up), or they keep oscillating. In ML terms, it's a point where you can't trust a slope.
What to learn next
- Measure change between two points in rate of change explained, the next lesson.
- See how limits define slopes in what is a derivative?.
- Revisit the sigmoid in sigmoid and softmax.