Numerical methods · continued fractions
Evaluating Fractions: Continued Fractions by Lentz's Method
A continued fraction is a nested expression of the form f = b₀ + a₁/(b₁ + a₂/(b₂ + a₃/(b₃ + …))). Many special functions have such expansions, among them the incomplete gamma and beta functions and the tangent, and for some arguments the fraction converges faster and more stably than a power series. The practical question is how to evaluate one on a computer when the number of levels needed is not known in advance. Lentz’s method answers it.
Evaluating the fraction from the inside out, by starting at a deep level and working back, forces a guess for the depth. Evaluating from the top by building numerator and denominator sequences works, but the intermediate numbers grow or shrink without bound and overflow. The modified Lentz algorithm avoids both problems by tracking ratios.
The algorithm
Keep three quantities: the running value f, and two helpers C and D. Start with f = b₀, C = f and D = 0. For each level j = 1, 2, …, update:
D = b_j + a_j · D D = 1 / D
C = b_j + a_j / C
Δ = C · D
f = f · Δ
stop when |Δ − 1| < ε
Each Δ is the factor by which the current convergent differs from the previous one, so the loop ends when a further level changes the value by less than the tolerance ε. If a denominator ever becomes zero, replace it by a tiny number, such as 10⁻³⁰, instead of dividing by it. That small substitution is the whole point of the modified version.
If b₀ itself is zero, start f at the same tiny number so the first multiplication does not wipe out everything.
Worked example: the golden ratio
The fraction 1 + 1/(1 + 1/(1 + …)) has every a_j = 1 and every b_j = 1, and its value is (1 + √5)/2 = 1.618034. Run the loop by hand. Start with f = 1, C = 1, D = 0.
- At j = 1: D = 1 + 0 = 1, so 1/D = 1; C = 1 + 1/1 = 2; Δ = 2; f = 2.
- At j = 2: D = 1 + 1 = 2, so 1/D = 0.5; C = 1 + 1/2 = 1.5; Δ = 0.75; f = 1.5.
- At j = 3: D = 1 + 0.5 = 1.5, so 1/D = 0.666667; C = 1 + 1/1.5 = 1.666667; Δ = 1.111111; f = 1.666667.
- At j = 4: D = 1 + 0.666667 = 1.666667, so 1/D = 0.6; C = 1 + 0.6 = 1.6; Δ = 0.96; f = 1.6.
- At j = 5: 1/D = 0.625, C = 1.625, Δ = 1.015625, f = 1.625.
- At j = 6: 1/D = 0.615385, C = 1.615385, Δ = 0.994083, f = 1.615385.
The values 1, 2, 3/2, 5/3, 8/5, 13/8 that appear are ratios of consecutive Fibonacci numbers, which the Fibonacci numbers page lists and the running quotient page computes directly. The error shrinks by a factor near 0.38 per step, so about fifteen levels give six digits.
A second example: the square root of 2
The fraction 1 + 1/(2 + 1/(2 + …)) has b₀ = 1 and every later a_j = 1, b_j = 2. The loop gives Δ = 1.5 at j = 1, so f = 1.5; at j = 2, D = 2 + 0.5 = 2.5 and C = 2 + 1/3 = 2.333333, so Δ = 0.933333 and f = 1.4; at j = 3, 1/D = 0.416667 and C = 2.428571, so Δ = 1.011905 and f = 1.416667. The limit is √2 = 1.414214, and the iterates bracket it from alternating sides.
Choosing the tolerance
A tolerance near 10⁻¹⁵ suits double precision. A cap on the number of levels, a few hundred for most functions, protects against fractions that converge slowly or not at all. When the cap is reached the routine should return an error rather than a half-converged number.
Where it is used
The incomplete gamma function switches to a continued fraction for large x, and the F and t tail functions on the F distribution tail page rely on one for the incomplete beta function. The partial numerators a_j in those fractions are formulas in the function arguments, not stored data. Related techniques for approximating functions by rational forms appear in the Padé approximation page, and the hub interpolation and calculus gathers neighboring methods.