NOTE: If the mathematical inserts in this vignette are not displaying correctly or even replaced by messages [Math processing error] it is most likely a caching problem with your browser. This may be solved by a “hard reload”, that is, by holding down the Shift key while clicking on the “reload/refresh” button. See, for example, this stackexchange query and reply.

1 Rational approximation by continued fractions

The notation we use is that of Khovanskii (1963), which is a fundamental reference.

A continued fraction is a development of the form:

\[ b_0 + \cfrac{a_1}{b_1 + \cfrac{a_2}{b_2 + \cfrac{a_3}{b_3 + \dotsb \vphantom{\cfrac{a_4}{b_4}}}}} \]

where the quantities \(a_1, a_2, \ldots\) are called the partial numerators and \(b_1, b_2, \dots\) the partial denominators. If \(b_0\) as well as the partial numerators and denominators are all integers, with the partial denominators positive, then terminating the development at any stage leads to a result that may be written as a ratio of two integers, that is, as a vulgar fraction. If the termination is after \(n\) steps, we write the result as

\[ \frac{P_n}{Q_n} = b_0 + \cfrac{a_1}{b_1 + \cfrac{a_2}{b_2 + \cfrac{a_3}{\begin{matrix} b_3 + \cdots\vphantom{\cfrac{1}{}} & \\ & b_{n-1} + \cfrac{a_n}{b_n} \end{matrix} }}} \]

Put \(P_0/Q_0 = b_0/1\). The ratios \(P_n/Q_n, n = 0, 1, 2, \dots\) are called the convergents of the continued fraction (whether they converge in the mathematical sense or not).

It is convenient to extend the series of convergents artificially further back one step and put:

\[ \frac{P_{-1}}{Q_{-1}} = \frac{1}{0}, \frac{P_0}{Q_0} = \frac{b_0}{1}, \frac{P_1}{Q_1}, \frac{P_2}{Q_2}, \cdots \]

With this definition, an easy inductive argument (Khovanskii 1963, 3) shows that the numerators and denominators of the convergents may be calculated progressively using a two-term recurrence relation. In fact the numerators and denominators have the same recurrence relations, namely:

\[\left. \begin{align} P_{n+1} & = b_{n+1}P_n + a_{n+1}P_{n-1} \\ Q_{n+1} & = b_{n+1}Q_n + a_{n+1}Q_{n-1} \end{align} \right\} \quad n = 0, 1, 2, \ldots \]

but of course \(P_n\) and \(Q_n\) have different starting values.

1.1 Real numbers into continued fractions

A rational approximation to a real number is an approximation in the form of the ratio of two integers, numerator and denominator. The denominator should be positive, and for definiteness we require the numerator and denominator to be relatively prime, that is with greatest common divisor equal to 1.

The simplest and most powerful to find rational approximation for real numbers is to express them as continued fractions, with the convergents forming the approximations. An algorithm to do this is most easily described as a series of steps.

  1. Let \(x\) be a real number for which the approximation is wanted.

  2. Write \(b_0 = \lfloor x\rfloor\) and put \(x = b_0 + (x - \lfloor x\rfloor) = b_0 + r_0\).

  3. \(0 \leq r_0 \lt 1\), by definition. There are two cases:

    • If \(r_0 = 0\) the process is complete. The rational approximation is exact.

    • If \(r_0 > 0\), note that \(1/r_0 > 1\). Write \(1/r_0 = \lfloor 1/r_0 \rfloor + (1/r_0 - \lfloor 1/r_0 \rfloor) = b_1+r_1\), with \(b_1 \geq 1\) and \(0 \leq r_1 < 1\). Then: \[ x = b_0 + \frac{1}{1/r_0} = b_0 + \cfrac{1}{b_1 + r_1}\]

  4. Continuing in this way we produce a continued fraction expansion for the real number of the form:

\[ x = b_0 + \cfrac{1}{b_1 + \cfrac{1}{b_2 + \cfrac{1}{b_3 + \dotsb \vphantom{\cfrac{1}{b_4}}}}} \]

where the partial numerators are all equal to 1 and the partial denominators are all positive integers. The fraction terminates if at any stage a ‘excess’ term, \(r_n\), becomes \(0\), preventing the process from continuing.

Continued fractions with all \(a_i=1,\; i = 1, 2, \ldots\) are usually called simple.

1.1.1 Some theoretical examples

In some special cases the process can be conducted theoretically, yielding the entire fraction. For example the “golden ratio”, \(\varphi = \left(\sqrt 5 + 1\right)/2\) is the positive root of the equation \(1/\varphi = \varphi - 1\). Writing this as \(\varphi - 1 = 1/\{1 + (\varphi-1)\}\) and iterating the right hand side leads directly to possibly the simplest continued fraction of the above form:

\[ \varphi = 1 + \cfrac{1}{1 + \cfrac{1}{1 + \cfrac{1}{1 + \cdots \vphantom{\cfrac{1}{1}}}}} \]

Further, the recurrence relations for the \(P_n\) and \(Q_n\) are out-of-step Fibonacci sequence relations. This shows the well-known result that \(\varphi\) is the limit of the ratio of consecutive Fibonacci numbers:

F <- c(1, 1, numeric(15))
for(i in 3:17) ## Fibonacci sequence by recurrence
  F[i] <- F[i-1] + F[i-2]  
F
 [1]    1    1    2    3    5    8   13   21   34   55   89  144  233  377
[15]  610  987 1597
vfractional((sqrt(5) + 1)/2, eps = 0, maxConv = 1:16)
 [1] 1        2        3/2      5/3      8/5      13/8     21/13   
 [8] 34/21    55/34    89/55    144/89   233/144  377/233  610/377 
[15] 987/610  1597/987

In a similar way we may write:

\[ \left(\sqrt 2 - 1\right) = \frac{\sqrt 2 - 1}{1}\times \frac{1 + \sqrt2}{1 + \sqrt2} = \frac{1}{1 + \sqrt2} = \frac{1}{2 + \left(\sqrt2 - 1\right)} \]

Iterating this equation in the same way, and slightly re-arranging, leads to the continued fraction:

\[ \sqrt 2 = 1 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{2 + \cdots \vphantom{\cfrac{1}{2}}}}} \]

One way to appreciate the algorithm is to look at how it might be coded in R.

The package provides no function to return the partial denominators, \(b_n\), explicitly. However writing such a bespoke function is simple.

partial_denominators <- function(x, k = 10) {
  b <- rep(NA, k)
  r <- x
  for(i in 1:k) {
    b[i] <- floor(r)
    r <- r - b[i]
    if(isTRUE(all.equal(r, 0))) break
    r <- 1/r
  }
  structure(b, names = paste0("b", 1:k-1))
}

To see it in action, we consider what it produces for the golden ratio, \(\varphi\), the circular ratio, \(\pi\), the base of natural logarithms, \(e\), and the square roots of the first few positive integers. Irrational numbers have periodic continued fraction expansions, so the patterns will become clear.

x <- c(pi = base::pi, e = exp(1), phi = (sqrt(5) + 1)/2, 
       structure(sqrt(1:9), names = paste0("sqrt(", 1:9, ")")))
tab <- x %>% sapply(partial_denominators) %>% t 
tab[is.na(tab)] <- ""
kable(tab, align = "r", caption = "Partial denominators")
Partial denominators
b0 b1 b2 b3 b4 b5 b6 b7 b8 b9
pi 3 7 15 1 292 1 1 1 2 1
e 2 1 2 1 1 4 1 1 6 1
phi 1 1 1 1 1 1 1 1 1 1
sqrt(1) 1
sqrt(2) 1 2 2 2 2 2 2 2 2 2
sqrt(3) 1 1 2 1 2 1 2 1 2 1
sqrt(4) 2
sqrt(5) 2 4 4 4 4 4 4 4 4 4
sqrt(6) 2 2 4 2 4 2 4 2 4 2
sqrt(7) 2 1 1 1 4 1 1 1 4 1
sqrt(8) 2 1 4 1 4 1 4 1 4 1
sqrt(9) 3

That \(\pi\) and \(e\) do not appear to have any stable periodic pattern is not surprising, as they are known to be transcendental rather than merely irrational. The Euler constant \(e\) has a pattern of period 3, but with the third term increasing by two in each cycle. By contrast the denominators for \(\pi\) appear to be entirely random1.

These, and many others are listed in the On-line Encyclopedia of Integer Sequences, (OIES).

2 Implementation in R

In R the implementation is simple. The following routine computes the convergents (as pairs of integers), terminating either when the error in the rational approximation is below a set tolerance, or a prescribed maximum number of convergents is reached. The return value is a vector of the final three values: \((P_n, Q_n, n)\).

.ratr <- function(x, eps = 1.0e-6, maxConv = 20) {
  PQ1 <- c(1, 0)
  PQ2 <- c(floor(x), 1)
  r <- x - PQ2[1]
  i <- 0
  while((i <- i+1) < maxConv && abs(x - PQ2[1]/PQ2[2]) > eps) {
    b <- floor(1/r)
    r <- 1/r - b
    PQ0 <- PQ1
    PQ1 <- PQ2
    PQ2 <- b*PQ1 + PQ0
  }
  return(c(PQ2, i-1))
}

We can check the result with a well-known rational approximation:

pq <- .ratr(pi)
cat("Pn = ", pq[1], ", Qn = ", pq[2], ", n = ", pq[3], "\n", sep = "")
cat("pi = ", format(pi, digits = 15), 
    ", Pn/Qn = ", format(pq[1]/pq[2], digits = 15), 
    ", Error = ", pi - pq[1]/pq[2], "\n", sep = "")
Pn = 355, Qn = 113, n = 3
pi = 3.14159265358979, Pn/Qn = 3.14159292035398, Error = -0.0000002667642

2.1 Some elementary properties

The convergents for a rational approximation constructed in this way have some interesting elementary properties. Again, all results are taken from Chapter 1 of Khovanskii (1963).

We can illustrate some of these properties by looking at the sequence of convergents the process generates for \(\pi\). Notice how the errors alternate in sign and rapidly decrease in absolute value. For reference, the accurate value of \(\pi\) is 3.141592653589793116.

n Pn/Qn value error
0 3 3.00000000000000 0.141592653589793
1 22/7 3.14285714285714 -0.001264489267350
2 333/106 3.14150943396226 0.000083219627529
3 355/113 3.14159292035398 -0.000000266764189
4 103993/33102 3.14159265301190 0.000000000577891
5 104348/33215 3.14159265392142 -0.000000000331628
6 208341/66317 3.14159265346744 0.000000000122356
7 312689/99532 3.14159265361894 -0.000000000029143
8 833719/265381 3.14159265358108 0.000000000008715
9 1146408/364913 3.14159265359140 -0.000000000001611

Given a fixed denominator, \(d\), the optimal rational approximation to \(x\) is clearly obtained by rounding \(x d\), that is using \(\left\lfloor xd+\frac12\right\rfloor/d\). The denominators \(Q_n\) of the convergents usually correspond to points where there is a sudden increase in accuracy. To illustrate this we consider approximating \(\sqrt 5\). The first few convergents are as follows:

(s5 <- vfractional(sqrt(5), eps = 0, maxConv = 1:7))
[1] 2          9/4        38/17      161/72     682/305    2889/1292 
[7] 12238/5473
d5 <- denominators(s5)
e5 <- abs(sqrt(5) - numerical(s5))

Now consider all rational approximations with denominators no larger:

d <- seq(max(d5))
n <- round(sqrt(5) * d)

To simplify the graph we may remove any which are not in their lowest terms.

gcd <- mapply(FUN = function(a, b) if(b == 0) a else Recall(b, a %% b),
              n, d)
nd <- cbind(n, d)/gcd
nd <- nd[!duplicated(nd), ]
e <- abs(sqrt(5) - nd[, 1]/nd[, 2])

To see the detail of what is happening, we need to use a log-log scale:

The blue points give the errors in the approximations for all denominators up to 5473; the red points and lines indicate the subset of them which are the continued fraction convergents.

Note that the convergent, \(P_n/Q_n\), is the most accurate approximation for any with denominator less that or equal to \(Q_n\), and is closer than at most one or two with denominators less than \(Q_{n+1}\).

References

Khovanskii, Alexey Nikolaevitch. 1963. The Application of Continued Fractions and Their Generalizations to Problems in Approximation Theory. P. Noordhoff N. V.


  1. \(\pi\) does have a regularly patterned continued fractions expression, but unlike \(e\), it appears to have no such simple continued fraction.