Skip to content

7.1 Derivatives and Finite Differences

Training a model by gradient descent needs the derivative of a loss with respect to every weight. The autograd engine built in Tensor, broadcasting backward, and no_grad and The op library and gradcheck_all in AI Engineering computes those derivatives by the chain rule, and every backward rule there could be wrong in ways that still train a little: a transposed gradient, a missing factor of 2, a flipped sign. The only independent check is the definition of the derivative itself, evaluated numerically: nudge one input, watch the output move, divide. That check is gradcheck, built in Gradients and gradcheck, and it rests on the functions you build in this section.

Concepts from other sections (8)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

  • chain rule: ddxf(g(x))=f′(g(x)) g′(x)\frac{d}{dx} f(g(x)) = f'(g(x))\, g'(x). Read more: 7.5 Calculus 1 Problem Set
  • central differences: the numerical derivative (f(x+ϵ)−f(x−ϵ))/2ϵ(f(x + \epsilon) - f(x - \epsilon))/2\epsilon, used as an oracle for analytic gradients. Read more: 6.3 The op library and gradcheck_all in AI Engineering
  • Taylor series: the power series ∑n=0∞f(n)(a)n!(x−a)n\sum_{n=0}^\infty \frac{f^{(n)}(a)}{n!}(x - a)^n built from the derivatives of ff at aa; truncating it gives the polynomials that libraries use to compute exe^x, sin⁡\sin, and cos⁡\cos. Read more: Chapter 8: Calculus 2
  • Gradcheck: a comparison of a hand-written gradient against central differences of ∑f(x)⊙g\sum f(x) \odot g. Read more: 3.3 Oracles, golden and differential tests, gradcheck as a test in Software Engineering
  • Gradient check: a comparison of an analytic gradient against the numerical one that reports pass or fail and the element furthest past its allowance. Read more: 10.1 Partial derivatives, gradients, and gradcheck
  • Automatic differentiation / autodiff: computing exact derivatives of a program by applying the chain rule to each primitive operation it executes, in forward mode (Jacobian-vector products) or reverse mode (vector-Jacobian products); it is how neural networks get their gradients, with no step size and no symbolic formula. Read more: Chapter 14: Matrix Calculus and Autodiff
  • logits: unnormalized log-scores that softmax turns into probabilities; here the table stores ln⁡P(j∣i)\ln P(j \mid i). Read more: 6.1 Byte bigram: counts, logits, safetensors v0 in AI Engineering
  • dual numbers: forward-mode autodiff values that carry an exact derivative alongside each number. Read more: 6.3 The op library and gradcheck_all in AI Engineering

A numerical derivative is only as trustworthy as its step size. Too large, and the formula is inaccurate; too small, and floating-point rounding swamps the answer. This section derives the right step from first principles, then implements the three difference formulas the rest of the book uses: the forward difference, the central difference, and Richardson extrapolation. The finished code lives in course/solutions/python/tinyllm/num/diff.py (functions forward_diff, central_diff, and richardson), and its contract is course/contracts/py/tinyllm/num/diff.pyi.

There is nothing to call from earlier sections, but the arguments lean on Exponents, logs, and units of information for exe^x and on Polynomials, Horner, and stable roots for the shape of a truncated polynomial expansion. As you read the implementation, try to predict each step before you see it.

Learning objectives

  • Derive the truncation error of the forward and central differences from Taylor’s formula and state their orders.
  • Explain why rounding error grows as the step shrinks, and compute the step that balances the two errors for each formula.
  • Scale a default step with the size of xx and divide by the step the hardware actually took.
  • Build a Richardson extrapolation table and explain why it uses weights 4k4^k.
  • Implement forward_diff, central_diff, and richardson with the validation the gradient checker relies on.

Throughout this section, ff is a function of one real variable evaluated in float64 (a Callable[[float], float]), xx is the point where you want the derivative, and h>0h > 0 is the step. The slope of the straight line through (x,f(x))(x, f(x)) and (x+h,f(x+h))(x + h, f(x + h)) is the difference quotient f(x+h)−f(x)h\frac{f(x+h) - f(x)}{h}: rise over run. As hh shrinks, the second point slides toward the first, and if the slopes settle on one number, that number is the derivative f′(x)f'(x), the slope of the tangent line:

f′(x)=lim⁡h→0f(x+h)−f(x)h.f'(x) = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h} .

“Settles on” has a precise meaning, the limit: for every tolerance you name, there is a step size below which every quotient is within that tolerance of f′(x)f'(x). For f(x)=x2f(x) = x^2 you get (x+h)2−x2h=2xh+h2h=2x+h\frac{(x+h)^2 - x^2}{h} = \frac{2xh + h^2}{h} = 2x + h, which tends to 2x2x. The usual rules follow from the definition the same way: (xn)′=nxn−1(x^n)' = n x^{n-1}, (ex)′=ex(e^x)' = e^x (the property that defines ee), (sin⁡x)′=cos⁡x(\sin x)' = \cos x, and the chain rule (f(g(x)))′=f′(g(x)) g′(x)(f(g(x)))' = f'(g(x))\, g'(x), which Jacobians and the chain rule generalizes and every backward pass applies.

A program picks one hh and computes one quotient. Two questions follow: how wrong is the quotient for a given hh, and which hh makes it least wrong? Both answers come from comparing ff near xx with a polynomial. If ff is smooth (has enough continuous derivatives), then for small hh

f(x+h)=f(x)+f′(x) h+12f′′(x) h2+16f′′′(x) h3+⋯f(x + h) = f(x) + f'(x)\, h + \tfrac{1}{2} f''(x)\, h^2 + \tfrac{1}{6} f'''(x)\, h^3 + \cdots

This is Taylor’s formula; Taylor series proves it and bounds the dots. Here only the first few terms matter, and you can check them on x3x^3: (x+h)3=x3+3x2h+3xh2+h3(x+h)^3 = x^3 + 3x^2 h + 3x h^2 + h^3, and indeed f′=3x2f' = 3x^2, 12f′′=3x\frac{1}{2} f'' = 3x, 16f′′′=1\frac{1}{6} f''' = 1.

With Taylor’s formula in hand, you can measure exactly how far each difference quotient sits from the true derivative.

Write D+(h)D_+(h) for the forward difference and D0(h)D_0(h) for the central difference, and use O(hp)O(h^p) to mean “at most a constant times hph^p” as h→0h \to 0. Subtract f(x)f(x) from Taylor’s formula and divide by hh:

D+(h)=f(x+h)−f(x)h=f′(x)+12f′′(x) h+O(h2).D_+(h) = \frac{f(x+h) - f(x)}{h} = f'(x) + \tfrac{1}{2} f''(x)\, h + O(h^2) .

The forward difference is off by about 12f′′(x)h\frac{1}{2} f''(x) h: it is first order, with error proportional to hh. Halve hh and the error halves. Now write the expansion at −h-h as well,

f(x−h)=f(x)−f′(x) h+12f′′(x) h2−16f′′′(x) h3+⋯ ,f(x - h) = f(x) - f'(x)\, h + \tfrac{1}{2} f''(x)\, h^2 - \tfrac{1}{6} f'''(x)\, h^3 + \cdots,

and subtract it from the one at +h+h. The even powers cancel:

D0(h)=f(x+h)−f(x−h)2h=f′(x)+16f′′′(x) h2+O(h4).D_0(h) = \frac{f(x+h) - f(x-h)}{2h} = f'(x) + \tfrac{1}{6} f'''(x)\, h^2 + O(h^4) .

The central difference is second order: halve hh and the error drops by a factor of 4. On a log-log plot of error against hh, the forward difference is a line of slope 1 and the central difference a line of slope 2; that slope is the signature of each formula’s order. The error from the dropped terms is called truncation error, because it comes from truncating the Taylor series. The central difference is exact on quadratics (f′′′=0f''' = 0), while the forward difference is exact only on straight lines.

If truncation were the whole story, you would simply take hh as small as possible. Floating point says otherwise.

Every float64 operation rounds its exact result to the nearest representable number, with relative error at most ε/2\varepsilon / 2, where ε=2−52≈2.2×10−16\varepsilon = 2^{-52} \approx 2.2 \times 10^{-16} is float64 machine epsilon, the gap between 1 and the next float. So a computed f(x+h)f(x + h) is off by about ε∣f(x)∣\varepsilon |f(x)|, more if ff itself is a long computation. When hh is small, the numerator f(x+h)−f(x−h)f(x+h) - f(x-h) is a difference of two nearly equal numbers: their leading digits cancel and their rounding errors do not. That error, about ε∣f∣\varepsilon |f|, is then divided by 2h2h. The total error of the central difference is

E0(h)≈16∣f′′′∣ h2⏟truncation+ε ∣f∣/h⏟rounding.E_0(h) \approx \underbrace{\tfrac{1}{6} |f'''|\, h^2}_{\text{truncation}} + \underbrace{\varepsilon\, |f| / h}_{\text{rounding}} .

The first term falls as hh shrinks, and the second rises. The following table shows the measured error of D0D_0 on exe^x at x=1x = 1, where e=2.71828…e = 2.71828\ldots:

hh10−110^{-1}10−210^{-2}10−310^{-3}10−410^{-4}10−510^{-5}10−610^{-6}10−810^{-8}10−1010^{-10}10−1210^{-12}
error of D0D_04.5×10−34.5 \times 10^{-3}4.5×10−54.5 \times 10^{-5}4.5×10−74.5 \times 10^{-7}4.5×10−94.5 \times 10^{-9}5.6×10−115.6 \times 10^{-11}9.1×10−119.1 \times 10^{-11}5.2×10−95.2 \times 10^{-9}9.0×10−79.0 \times 10^{-7}1.2×10−41.2 \times 10^{-4}

Down to 10−410^{-4} the error falls by a factor of 100 per decade (slope 2); below about 10−510^{-5} rounding takes over and the error rises again. A step of 10−1210^{-12} is worse than a step of 10−210^{-2}.

Since one error falls and the other rises, there is a best step in between. Minimize E0(h)=ah2+b/hE_0(h) = a h^2 + b / h with a=∣f′′′∣/6a = |f'''|/6 and b=ε∣f∣b = \varepsilon |f|: the derivative 2ah−b/h22ah - b/h^2 is zero at h⋆=(b/2a)1/3h^\star = (b / 2a)^{1/3}. When ff and its derivatives are of size 1, that is h⋆≈ε3≈6.1×10−6h^\star \approx \sqrt[3]{\varepsilon} \approx 6.1 \times 10^{-6}, and the error there is about ε2/3≈3.7×10−11\varepsilon^{2/3} \approx 3.7 \times 10^{-11}: two thirds of the 16 digits survive. The same argument for the forward difference, E+(h)≈12∣f′′∣h+ε∣f∣/hE_+(h) \approx \frac{1}{2}|f''| h + \varepsilon |f| / h, gives h⋆≈ε≈1.5×10−8h^\star \approx \sqrt{\varepsilon} \approx 1.5 \times 10^{-8} and an error of about ε\sqrt{\varepsilon}, half the digits.

The step must also follow the scale of xx. Floats near xx are about ε∣x∣\varepsilon |x| apart, so at x=108x = 10^8 a step of 6×10−66 \times 10^{-6} moves xx by only 6×10−146 \times 10^{-14} relative, and the quotient is mostly rounding. A step proportional to ∣x∣|x| keeps the relative perturbation fixed, and using max⁡(1,∣x∣)\max(1, |x|) instead of ∣x∣|x| keeps it from vanishing at x=0x = 0. The contract’s defaults are therefore

h+=ε max⁡(1,∣x∣),h0=ε3 max⁡(1,∣x∣).h_+ = \sqrt{\varepsilon}\, \max(1, |x|), \qquad h_0 = \sqrt[3]{\varepsilon}\, \max(1, |x|) .

One more floating-point detail matters. x+hx + h is itself rounded, so the step the hardware took is (x+h)−x(x + h) - x, not hh. Dividing by the step actually taken makes f(x)=xf(x) = x come out as exactly 1. With x=0.1x = 0.1 and h=10−5h = 10^{-5}, dividing by 2h2h gives 0.99999999999960.9999999999996 instead.

The best single central difference keeps about 11 digits. You can do better by removing truncation error with algebra instead of a smaller step. The central difference has an error series with only even powers, D0(h)=f′+c2h2+c4h4+⋯D_0(h) = f' + c_2 h^2 + c_4 h^4 + \cdots, where the cc‘s do not depend on hh. Then D0(h/2)=f′+c2h2/4+c4h4/16+⋯D_0(h/2) = f' + c_2 h^2/4 + c_4 h^4/16 + \cdots, and the combination

R1(h)=4 D0(h/2)−D0(h)4−1=f′+O(h4)R_1(h) = \frac{4\, D_0(h/2) - D_0(h)}{4 - 1} = f' + O(h^4)

cancels the h2h^2 term exactly. Repeating with weight 4k4^k at level kk cancels h2kh^{2k}. Writing D[0][j]=D0(h/2j)D[0][j] = D_0(h / 2^j) and

D[k][j]=4kD[k−1][j+1]−D[k−1][j]4k−1,D[k][j] = \frac{4^k D[k-1][j+1] - D[k-1][j]}{4^k - 1},

the value RL(h)R_L(h) after LL levels has error O(h2L+2)O(h^{2L+2}) and is exact on polynomials of degree up to 2L+22L + 2. Because the truncation error shrinks so fast, Richardson can use a large hh (like 0.1), where rounding is negligible, and still reach about 12 digits. This is the method behind adaptive differentiation libraries.

Before writing code, check every formula on an example small enough to compute by hand. Take f(x)=x3f(x) = x^3 at x=2x = 2, so f′(2)=3⋅4=12f'(2) = 3 \cdot 4 = 12, with h=0.1h = 0.1. The function values are f(2.1)=9.261f(2.1) = 9.261, f(2)=8f(2) = 8, f(1.9)=6.859f(1.9) = 6.859, f(2.05)=8.615125f(2.05) = 8.615125, and f(1.95)=7.414875f(1.95) = 7.414875. The table collects the four estimates:

QuantityComputationValueError
D+(0.1)D_+(0.1)(9.261−8)/0.1(9.261 - 8) / 0.112.610.61=3⋅2⋅0.1+0.120.61 = 3 \cdot 2 \cdot 0.1 + 0.1^2
D0(0.1)D_0(0.1)(9.261−6.859)/0.2(9.261 - 6.859) / 0.212.010.01=h20.01 = h^2 (here 16f′′′=1\frac{1}{6} f''' = 1)
D0(0.05)D_0(0.05)(8.615125−7.414875)/0.1(8.615125 - 7.414875) / 0.112.00250.0025=(h/2)20.0025 = (h/2)^2
R1(0.1)R_1(0.1)(4⋅12.0025−12.01)/3=36/3(4 \cdot 12.0025 - 12.01) / 3 = 36 / 3120

The forward error 0.610.61 is the 12f′′h=3xh=0.6\frac{1}{2} f'' h = 3xh = 0.6 predicted by the truncation analysis, plus the next term h2=0.01h^2 = 0.01. The central error is exactly h2h^2 because x3x^3 has no terms beyond h3h^3, and quartering it at h/2h/2 is what lets Richardson remove it completely.

The three functions share one shape:

def forward_diff(f: Callable[[float], float], x: float, h: Optional[float] = None) -> float: ...
def central_diff(f: Callable[[float], float], x: float, h: Optional[float] = None) -> float: ...
def richardson(f: Callable[[float], float], x: float, h: float, levels: int = 2) -> float: ...

h = None means the default step derived above; a given h is used as is, because the gradient checker passes its own. Each function returns a Python float, divides by the step actually taken, and raises ValueError for a non-finite x, a step that is not finite and positive, or levels < 0. Richardson’s default is two levels.

The design follows from the analysis in seven decisions.

  1. Pick the formula by its truncation order. The forward difference errs by 12f′′h\tfrac12 f'' h, the central one by 16f′′′h2\tfrac16 f''' h^2. Both are provided because callers differ (an optimizer that already has f(x)f(x) saves one evaluation with the forward one), but gradcheck uses the central one.
  2. Pick the default step by balancing errors. Minimizing ahp+b/ha h^p + b/h gives h⋆∝ε1/(p+1)h^\star \propto \varepsilon^{1/(p+1)}, so ε\sqrt{\varepsilon} for the forward difference (p=1p = 1) and ε3\sqrt[3]{\varepsilon} for the central one (p=2p = 2). These are separate constants because each is wrong for the other formula.
  3. Scale the step with xx, but never to zero. The spacing of floats near xx is about ε∣x∣\varepsilon |x|, so a fixed relative step means h∝∣x∣h \propto |x|. Multiplying by max⁡(1,∣x∣)\max(1, |x|) instead of ∣x∣|x| keeps hh from vanishing at x=0x = 0.
  4. Treat a caller’s step as sacred. gradcheck chooses eps deliberately, so an explicit h is validated (finite and positive) and used unchanged; only None triggers the default.
  5. Divide by the step the hardware took. x+hx + h is rounded, so the true run of the secant is xp - x (or xp - xm), not hh (or 2h2h). Dividing by it makes the quotient the exact slope of the secant through the two evaluated points, which is why f(x)=xf(x) = x gives exactly 1.
  6. Build Richardson as a table, column by column. Column 0 holds D0(h/2j)D_0(h/2^j) for j=0,…,Lj = 0, \ldots, L. Each later column applies 4kD[k−1][j+1]−D[k−1][j]4k−1\frac{4^k D[k-1][j+1] - D[k-1][j]}{4^k - 1}, which shortens the column by one, so after LL rounds a single value remains. Only the current column needs to be kept.
  7. Validate at the boundary. NaN or infinite xx, a bad step, or a negative or fractional levels raises immediately, so a NaN never travels silently into a gradient check.

The code starts with the validators and the machine epsilon constant:

EPS = 2.0**-52 # float64 machine epsilon: the gap between 1 and the next float
def _check_x(x: float) -> float:
x = float(x)
if not math.isfinite(x): # nan and +-inf have no derivative to estimate
raise ValueError(f"x must be finite, got {x}")
return x
def _check_h(h: float) -> float:
h = float(h)
if not math.isfinite(h) or h <= 0: # "not isfinite" first, so nan is rejected too
raise ValueError(f"h must be finite and > 0, got {h}")
return h

The two difference formulas differ only in their default step and their second point:

def forward_diff(
f: Callable[[float], float], x: float, h: Optional[float] = None
) -> float:
x = _check_x(x)
# Default: sqrt(eps), the balance point for an O(h) formula, relative to |x|.
h = math.sqrt(EPS) * max(1.0, abs(x)) if h is None else _check_h(h)
xp = x + h
# Divide by the step the hardware actually took: x + h is rounded, so
# (x + h) - x is usually not h. Using it makes f(x) = x exact.
return (float(f(xp)) - float(f(x))) / (xp - x)
def central_diff(
f: Callable[[float], float], x: float, h: Optional[float] = None
) -> float:
x = _check_x(x)
# Default: cbrt(eps), the balance point for an O(h^2) formula.
h = math.cbrt(EPS) * max(1.0, abs(x)) if h is None else _check_h(h)
xp, xm = x + h, x - h
# xp - xm is the run actually taken, not 2h.
return (float(f(xp)) - float(f(xm))) / (xp - xm)

float(f(...)) turns a numpy scalar returned by f into a Python float before the subtraction, so the result is always a plain float. At x=108x = 10^8 the default central step is 6.1×10−6⋅108=6106.1 \times 10^{-6} \cdot 10^8 = 610, which perturbs xx by the same relative amount as 6.1×10−66.1 \times 10^{-6} perturbs 1.

Richardson builds on central_diff with explicit steps:

def richardson(
f: Callable[[float], float], x: float, h: float, levels: int = 2
) -> float:
x = _check_x(x)
h = _check_h(h) # no default: Richardson wants a large h
if int(levels) != levels or levels < 0:
raise ValueError(f"levels must be an integer >= 0, got {levels}")
levels = int(levels)
# Column 0: central differences at h, h/2, h/4, ... Each later column
# cancels the next even power of h: D(h) = f' + c2 h^2 + c4 h^4 + ...,
# so (4^k D(h/2) - D(h)) / (4^k - 1) removes the h^(2k) term.
col = [central_diff(f, x, h / 2.0**j) for j in range(levels + 1)]
for k in range(1, levels + 1):
p = 4.0**k
# Combine neighbours: entry j uses steps h/2^j and h/2^(j+1).
col = [(p * col[j + 1] - col[j]) / (p - 1.0) for j in range(len(col) - 1)]
return col[0] # the single value of the last column

On the hand-worked example with levels=1, col starts as [12.01,12.0025][12.01, 12.0025]; with p=4p = 4 the next column is [(4⋅12.0025−12.01)/3]=[12][(4 \cdot 12.0025 - 12.01)/3] = [12], exact. levels=0 returns the plain central difference D0(h)D_0(h). The full solution is in diff.py.

Consider cost first. forward_diff evaluates ff twice, central_diff twice, and richardson 2(L+1)2(L + 1) times, plus O(L2)O(L^2) arithmetic for the table. For gradcheck on nn parameters that is 2n2n evaluations of the loss, which is why gradcheck runs on small models only.

The central difference beats the forward one for checking gradients. At its best step it keeps about ε2/3\varepsilon^{2/3} (11 digits), against ε\sqrt{\varepsilon} (8 digits) for the forward one, for the same two evaluations. gradcheck compares against an analytic gradient with a tolerance, so the extra digits decide whether real bugs stand out from noise.

Richardson beats a smaller step. A smaller hh eventually makes rounding dominate, as the error table for exe^x showed. Richardson reduces truncation error by algebra instead, so it can keep hh large where rounding is negligible and reach about 12 digits with three pairs of evaluations.

Two alternatives were not taken. Complex-step differentiation has no subtraction and is exact to rounding, but it needs ff to accept complex input, which most of the book’s code does not. Automatic differentiation is exact, but it is precisely what these functions exist to check, so it cannot be its own referee.

The design also accepts two tradeoffs. The default step assumes ff and its derivatives have size near 1 relative to max⁡(1,∣x∣)\max(1,|x|); a badly scaled ff (huge f′′′f''') would want a different step, and callers that know better pass h. And richardson divides each column by the step actually taken too, so its steps are only approximately in ratio 2; the difference is of rounding size and does not affect the cancellation.

A correct implementation must reproduce the hand-worked values 12.61, 12.01, and 12, so that everyone agrees on the definitions, and it must return a Python float even when f returns numpy scalars, because gradcheck stores results in float64 arrays. Its error behavior is tested as properties: the log-log slope of the central error must be 2 and that of the forward error 1, confirming the truncation orders. At the default steps, the central error must be at most 10−910^{-9} on five smooth functions at 26 points (the tolerance in Gradients and gradcheck assumes this accuracy), and the forward error at most 10−710^{-7}, the ε\sqrt{\varepsilon} rule at work.

The boundaries matter just as much. The default step must scale with xx: ddxlog⁡x\frac{d}{dx}\log x at x=108x = 10^8 must come out to 7 digits, because weights and logits are not all near 1. The default step at x=0x = 0 must be nonzero, because activations are checked at their kink. f(x)=xf(x) = x must give exactly 1, which only dividing by the step actually taken achieves. A given step must be used as is: the central difference of x3x^3 with h = 0.5 at x=100x = 100 gives 3⋅1002+0.52=30000.253 \cdot 100^2 + 0.5^2 = 30000.25, not the default-step answer, so gradcheck’s eps means what it says. For Richardson, on exe^x at 0.5 with h=0.1h = 0.1, the errors for levels 0 to 3 must fall as 2.7×10−32.7 \times 10^{-3}, 3.4×10−73.4 \times 10^{-7}, 5×10−125 \times 10^{-12}, and 10−1410^{-14}; it must be exact on a quartic with one level and a sextic with two, confirming the O(h2L+2)O(h^{2L+2}) claim. Finally, h=0h = 0, a negative hh, NaN, or infinity must raise ValueError, as must a NaN or infinite xx or negative levels, so bugs surface at the call and NaN never travels into a gradient check.

The common mistakes each have a recognizable symptom, and the table maps them to the line of the solution that prevents them:

PitfallSymptomHow the solution avoids it
a one-sided difference where the central one was meanterror 10−610^{-6} instead of 10−1110^{-11}; slope 1 on the log-log plotcentral_diff evaluates at x + h and x - h
an absolute step, ignoring the size of xxthe derivative of log⁡\log at 10810^8 is off by 3 percentdefault step multiplied by max(1.0, abs(x))
a step proportional to ∣x∣\lvert x \rvert aloneh=0h = 0 at x=0x = 0: division by zerothe max(1.0, ...) floor
dividing by 2h2h instead of the step actually takenf(x)=xf(x) = x differentiates to 0.99999999999960.9999999999996divides by xp - xm (or xp - x)
the wrong step rule for the formula (ε\sqrt{\varepsilon} for central, ε3\sqrt[3]{\varepsilon} for forward)central error 6×10−96 \times 10^{-9} instead of 10−1010^{-10}; forward error 3×10−63 \times 10^{-6} instead of 10−810^{-8}math.sqrt(EPS) in forward_diff, math.cbrt(EPS) in central_diff
Richardson weights 2k2^k instead of 4k4^kthe extrapolation removes nothing; worse than level 0p = 4.0**k
Richardson steps that grow (h⋅2jh \cdot 2^j) instead of shrinkthe hand example gives 12.05column 0 uses h / 2.0**j
rescaling a step the caller gavegradcheck’s eps silently becomes 100⋅100 \cdot eps at x=100x = 100the default applies only when h is None
accepting a NaN or infinite stepa NaN derivative instead of an error_check_h rejects non-finite and non-positive steps

Warning: The most damaging of these is silent: rescaling a caller’s step. Nothing crashes, but gradcheck’s tolerance stops meaning what its author intended.

These functions are used throughout the book. Gradients and gradcheck builds numerical_grad by calling central_diff(along, x_i, eps) once per coordinate of every input: the frozen-step gradient behind gradcheck. The next section, Newton’s Method, uses central_diff as its slope when it is given no derivative. Taylor series proves Taylor’s formula with a remainder, making the truncation errors here rigorous, and Error analysis, condition numbers, and tolerance budgets generalizes the rounding analysis. In AI Engineering, F.gradcheck_all() in The op library and gradcheck_all checks every op of the autograd library through gradcheck, and so through this file. Exact derivatives with no step at all come from automatic differentiation: the autograd engine and the dual numbers of Dual numbers and forward-mode autodiff.

  • OpenStax, Calculus Volume 1 (free), sections 2.2 and 3.1 to 3.2: limits and the derivative.
  • Sauer, Numerical Analysis, section 5.1: numerical differentiation and its rounding error.
  • Nocedal and Wright, Numerical Optimization, section 8.1: finite-difference gradients.
  • PyTorch torch.autograd.gradcheck (torch/autograd/gradcheck.py, _compute_numerical_gradient): the same central difference per element in float64 with eps 10−610^{-6}, plus checks of complex inputs and forward-mode gradients.
  • SciPy scipy.optimize.approx_fprime and scipy.differentiate.derivative (scipy/optimize/_numdiff.py): forward steps for optimizers, with adaptive step and order selection.
  • numdifftools (numdifftools/limits.py): Richardson tables with automatic step search and error estimates.
  • Martins, Sturdza, and Alonso, “The complex-step derivative approximation” (2003): f′(x)≈Im⁡f(x+ih)/hf'(x) \approx \operatorname{Im} f(x + ih)/h has no subtraction, so h=10−200h = 10^{-200} works and the result is exact to rounding.
  • derivative: the limit of the difference quotient f(x+h)−f(x)h\frac{f(x+h) - f(x)}{h} as h→0h \to 0; the slope of the tangent line.
  • forward difference: D+(h)=f(x+h)−f(x)hD_+(h) = \frac{f(x+h) - f(x)}{h}, a first-order approximation of f′(x)f'(x).
  • central difference: D0(h)=f(x+h)−f(x−h)2hD_0(h) = \frac{f(x+h) - f(x-h)}{2h}, a second-order approximation of f′(x)f'(x).
  • truncation error: the error from dropping the higher terms of the Taylor series.
  • rounding error: the error from floating-point rounding, about ε∣f∣/h\varepsilon |f| / h in a difference quotient.
  • machine epsilon: ε=2−52\varepsilon = 2^{-52}, the gap between 1 and the next float64.
  • Richardson extrapolation: combining differences at h,h/2,h/4,…h, h/2, h/4, \ldots with weights 4k4^k to cancel successive even powers of hh.
  • The derivative is a limit; a computer evaluates the quotient at one h>0h > 0, and the result is an approximation whose error you can predict.
  • The forward difference has truncation error proportional to hh; the central difference cancels the hh term and has error proportional to h2h^2.
  • Rounding adds an error of about ε∣f∣/h\varepsilon |f| / h that grows as hh shrinks, so the best step balances the two: h≈εh \approx \sqrt{\varepsilon} for forward, h≈ε3h \approx \sqrt[3]{\varepsilon} for central, times max⁡(1,∣x∣)\max(1, |x|).
  • Divide by the step actually taken, (x+h)−(x−h)(x+h) - (x-h), not by 2h2h, because x+hx + h is rounded.
  • Richardson extrapolation combines central differences at h,h/2,h/4,…h, h/2, h/4, \ldots to cancel h2,h4,…h^2, h^4, \ldots in turn, reaching about 12 digits from a large step.
  1. Why is the default step ε3\sqrt[3]{\varepsilon} for the central difference but ε\sqrt{\varepsilon} for the forward one?

    Answer

    Each step minimizes truncation plus rounding, ahp+ε∣f∣/ha h^p + \varepsilon |f| / h. Setting the derivative to zero gives h∝ε1/(p+1)h \propto \varepsilon^{1/(p+1)}. The central difference has p=2p = 2, so h≈ε1/3≈6×10−6h \approx \varepsilon^{1/3} \approx 6 \times 10^{-6}; the forward one has p=1p = 1, so h≈ε1/2≈1.5×10−8h \approx \varepsilon^{1/2} \approx 1.5 \times 10^{-8}. Using the other formula’s step leaves either too much truncation or too much rounding.

  2. What happens to central_diff(math.log, 1e8) if the step is ε3\sqrt[3]{\varepsilon} without the max⁡(1,∣x∣)\max(1, |x|) factor?

    Answer

    x+6×10−6x + 6 \times 10^{-6} near 10810^8 moves xx by less than one part in 101310^{13}, a handful of float spacings (about 1.5×10−81.5 \times 10^{-8} each). The numerator log⁡(x+h)−log⁡(x−h)\log(x+h) - \log(x-h) is then mostly rounding, and the derivative comes out several percent wrong. Scaling the step to 6×10−6⋅1086 \times 10^{-6} \cdot 10^8 restores the relative perturbation of the x=1x = 1 case.

  3. Why divide by xp - xm rather than 2 * h?

    Answer

    xp and xm are rounded, so the points where ff was evaluated are not exactly 2h2h apart. The quotient is meant to be the slope of the secant through the two points actually used; dividing by their actual distance makes it so, which is why f(x)=xf(x) = x gives exactly 1. Dividing by 2h2h adds a relative error of up to about ε∣x∣/h\varepsilon |x| / h; at x=0.1x = 0.1, h=10−5h = 10^{-5} it gives 0.99999999999960.9999999999996 instead of 1.

  4. Why does Richardson combine levels with 4k4^k and not 2k2^k?

    Answer

    The central difference’s error series has only even powers, c2h2+c4h4+⋯c_2 h^2 + c_4 h^4 + \cdots. Halving the step divides the h2kh^{2k} term by 22k=4k2^{2k} = 4^k, so 4kD(h/2)−D(h)4^k D(h/2) - D(h) cancels it exactly. With 2k2^k the leading term does not cancel and the extrapolation is worse than not extrapolating.

  5. Why does richardson require h while the other two functions default it?

    Answer

    The defaults are tuned to make a single difference quotient as accurate as possible, which puts hh where rounding and truncation are equal. Richardson’s point is the opposite regime: a large hh (like 0.1) where rounding is negligible, letting extrapolation remove the truncation. Its later columns also amplify rounding by the weights 4k/(4k−1)4^k/(4^k-1), so starting from a tiny default step would undo its advantage.