On this page:
2.1 Introduction
2.2 Precision
2.3 The Arithmetic of Continued Fractions
2.4 Purpose

2 Concepts🔗ℹ

2.1 Introduction🔗ℹ

A simple continued fraction is a number of the form a+1/(b+1/(c+...)), possibly infinite. They have the convenient property that terminating the expression at some point results in a rational that is "close" to the whole expression, in the sense that there is no rational closer with a smaller denominator. For instance, 14/3 = 4 + 2/3 = 4 + 1/(3/2) = 4 + 1/(1 + 1/2). When all the numerators are 1 like this, we can simply write (4 1 2). This process will always terminate for rational numbers since it is equivalent to the Euclidean algorithm.

Real solutions to quadratic equations with integer coefficients have repeating continued fractions. For example, √2 = (1; 2 ...) which is 1 + 1/(2 + 1/(2 + ...)). Other algebraics do not have a repeating simple continued fraction.

Simple continued fractions are unique, given a few caveats. As you can see from the 14/3 example, 4 + 1/(1 + 1/2) = 4 + 1/(1 + 1/(1 + 1)), which is to say that (a b ... c) is the same as (a b ... c-1 1). Additionally, interspersed zeroes can be put in such that (a b 0 n c ...) is the same as (a b+n c ...). Lastly, the representation of negative numbers is not unique, either. If B is a [possibly infinite] continued fraction (a b c ...) then there are two ways to write -B. The first is (-a -b -c ...) and the second is (-(a+1) 1 |b|-1 |c| ...). This library prefers the second form.

Many common functions or numbers have a continued fraction representation where the numerators are not all 1s, such as the continued fraction for pi which can be written 0 + 4/(1 + 1^2/(3 + 2^2/(5 + 3^2/(7 + ...)))). Such representations are not unique.

2.2 Precision🔗ℹ

When considered as a regular continued fraction, terminating the continued fraction at some point yields a rational n/d, called a "convergent." Then the absolute value of the difference between this rational and the value of the full continued fraction is less than one divided by the product of the denominators of this convergent and the next best convergent. This factor is a worst-case scenario, most continued fractions have convergents with much better precision. In fact, the golden ratio φ = (1+√5)/2 is the worst case, constantly staying just inside the bounds. Because of this, the golden ratio is sometimes called the "most irrational" number; that is, the number that is hardest to approximate by rationals.

2.3 The Arithmetic of Continued Fractions🔗ℹ

Continued fractions are amenable to term-at-a-time arithmetic. Suppose we have a function f(x) = (ax+b)/(cx+d), where x is a continued fraction of the form x = t + 1/x’, x’ being the rest of the continued fraction. Then when f consumes the leading term t, x goes to t+1/x’, which after multiplication to get f back in the same form, has updated the coefficients. If the continued fraction x is in standard form, then we have guarantees about every term except the first nonzero term, namely, that it will lie somewhere between 1 and infinity (a terminated continued fraction). Then if the limits of f as x goes to 1 and as x goes to infinity agree it does not matter what the value of x’ is as the leading term of f is already determined. We can take a quotient term out of the function, and f then goes to t + 1/f’.

For further details, reference the MIT HAKMEM memo 239 (Gosper).

Many common functions have continued fraction representations: general continued fractions whose numerators and denominators are polynomials in x with integer coefficients, that is, elements of the ring Z[x]. Thus if we can represent integer polynomials we can accept continued fraction arguments to continued fraction functions, since the input continued fraction comes with its own inherent bounds and therefore the function itself will have some known bounds, and when those bounds are tight we can emit a term.

This is expensive compared to continued fraction arithmetic on integers. It has been optimized, though not exhaustively: most of the time now goes to arithmetic on polynomials whose integer coefficients grow with every term read, which is inherent to the method. This cost is only paid when arguments to continued fraction functions are themselves continued fractions; ordinary continued fraction arithmetic on exact rational values is not impacted by this.

2.4 Purpose🔗ℹ

Because of the term-at-a-time aspect of continued fraction arithmetic, calculating terms and quotients proceeds without unnecessary calculation. Because of the precision guarantees, calculation can be terminated when the desired precision is exceeded. Combining these facts give us a picture of exact arithmetic of rationals and irrationals which only calculate as far as needed and yield the rational with the smallest denominator possible for the given precision.