L'Hôpital's Rule
L'Hôpital's rule evaluates limits that come out as or by comparing how fast the top and bottom change: replace both by their derivatives and try again. It turns many "indeterminate" limits into one-line calculations, and it explains which functions win in a growth race.
The rule
Suppose and (or both ) as , and , are differentiable near with there. Then
It also works for one-sided limits and for . Note: you differentiate the top and bottom separately. This is not the quotient rule.
Why it works, for the case with : near , both functions look like their tangent lines,
Examples
As the second example shows, you may apply the rule repeatedly while the form stays indeterminate.
import sympy as sp
x = sp.symbols('x')
cases = [(sp.sin(3 * x), x, 0), (sp.exp(x) - 1 - x, x**2, 0),
(sp.log(x), x, sp.oo), (x**3, sp.exp(x), sp.oo)]
for f, g, a in cases:
once = sp.limit(sp.diff(f, x) / sp.diff(g, x), x, a)
print(f"{str(f / g):<24} direct: {sp.limit(f / g, x, a)!s:<5} after one L'Hôpital step: {once}")sin(3*x)/x direct: 3 after one L'Hôpital step: 3
(-x + exp(x) - 1)/x**2 direct: 1/2 after one L'Hôpital step: 1/2
log(x)/x direct: 0 after one L'Hôpital step: 0
x**3*exp(-x) direct: 0 after one L'Hôpital step: 0
Numeric evidence
The second example from the table, approached through shrinking . Because subtracts nearly equal numbers, the direct formula loses precision for small . math.expm1 computes accurately and fixes it:
import math
print(" x naive (e^x-1-x)/x^2 with expm1")
for k in range(1, 9):
x = 10.0**-k
naive = (math.exp(x) - 1 - x) / x**2
stable = (math.expm1(x) - x) / x**2
print(f"{x:8.0e} {naive:20.12f} {stable:.12f}") x naive (e^x-1-x)/x^2 with expm1
1e-01 0.517091807565 0.517091807565
1e-02 0.501670841679 0.501670841681
1e-03 0.500166708385 0.500166708342
1e-04 0.500016671409 0.500016667083
1e-05 0.500000696482 0.500001666674
1e-06 0.499962183698 0.500000166562
1e-07 0.494336803056 0.500000016372
1e-08 -0.607747099184 0.500000008431
The stable column settles on 0.5 as L'Hôpital predicts; the naive column drifts away once rounding error (about ) dominates. This is the floating-point lesson again.
Other indeterminate forms
, , , and must be rewritten as a fraction first.
| Form | Trick | Example |
|---|---|---|
| write | as : | |
| common denominator | → combine, then L'Hôpital | |
| , , | take , find the limit of the exponent | : , so the limit is |
import sympy as sp
x = sp.symbols('x', positive=True)
print("x*ln(x), x->0+ :", sp.limit(x * sp.log(x), x, 0, dir='+'))
print("x**x, x->0+ :", sp.limit(x**x, x, 0, dir='+'))
print("(1+2/x)**x :", sp.limit((1 + 2 / x)**x, x, sp.oo))
print("1/x - 1/sin(x) :", sp.limit(1 / x - 1 / sp.sin(x), x, 0))x*ln(x), x->0+ : 0
x**x, x->0+ : 1
(1+2/x)**x : exp(2)
1/x - 1/sin(x) : 0
Note: Check the form first. as is simply ; blindly applying L'Hôpital would give , which is wrong.
In software
- Comparing algorithms: by L'Hôpital, which is the precise sense in which beats . The same argument shows any polynomial is eventually beaten by an exponential, the reason brute-force search does not scale (DSA).
- Removable singularities in code: functions like (the sinc function used in signal processing and image resampling), and (in entropy and cross-entropy, where is defined as 0) have limits that libraries hard-code at the bad point. L'Hôpital tells you the right value to plug in.
- Numerically stable formulas: knowing the limit tells you when to switch to a series or a special function (
expm1,log1p,np.sinc) instead of evaluating a formula that cancels catastrophically.
Exercises
Exercise 1: A basic
Find as with L'Hôpital and check it by factoring.
Answer
6
Solution
Exercise 2: Twice
Find as .
Answer
Solution
Exercise 3:
Find as .
Answer
0
Solution
Exercise 4: The form
Find as (continuous compounding at 5%), and check numerically with .
Answer
Solution
import math
x = 1e6
print(round((1 + 0.05 / x) ** x, 6), round(math.exp(0.05), 6))1.051271 1.051271
Test yourself
Which limit can L'Hôpital's rule be applied to directly?
- as
- as
- as
- as
What is as ?
In L'Hôpital's rule, what do you differentiate?
- The quotient with the quotient rule
- The numerator and denominator separately
- Only the numerator
- Only the denominator