Andreas is a Ph.D. student in the University of Cambridge's Department of Pure Mathematics and Mathematical Statistics. He can be contacted at A.O.Bender@ pmms.cam.ac.uk.
Introduction
Speed and accuracy are common concerns in the study of algorithms. In
time-critical code, it can even be difficult to do accurate arithmetic.
Say, for example, you need to multiply by
in a
fixed-point computation. One obvious approach is to convert
into a fixed-point number, and do a single
multiplication. Unfortunately, accuracy requires working with large
intermediate values. It's often better to multiply by a small value (to
avoid overflow) and divide by another small value. If you choose your
fraction carefully, you can actually obtain better precision than with
the fixed-point approach.
In his October 1995 "Algorithm Alley," Louis Plebani examined one way of finding such fractions using Farey series. This month, Andreas Bender takes a closer look at the Farey series and presents an alternative approach based on continued fractions.
Writing a number such as ".12345" as a fraction is entirely
straightforward--
=12345/100000. However, what
fraction best approximates
if its numerator or
denominator may not exceed a given size H? How small can the
numerator or denominator of the approximating fraction be if we can
tolerate an error of at most
.
Careful readers of this column will remember that Louis Plebani addressed these questions in his article "Common-Fraction Approximation of Real Numbers" (DDJ, October 1995). Instead of the Farey fractions he used, however, I'll approach the problem using continued fractions, then compare the merits of the two methods. While I hope that this article is reasonably self contained, it will certainly be easier to follow if you have read Plebani's article as well.
What is a continued fraction? Figure 1
provides a concise definition. To build a continued-fraction
approximation to a rational number, we apply the Euclidean algorithm to
the numerator and denominator of
=r/s; see Figure
2. Since
<1, q0 is
equal to 0. The numbers qi are called the
"partial quotients." Truncating the continued fraction of Figure 1 at qi leaves
the "complete quotient"
ni/di. For example,
the complete quotients of 12/17 are
0/1, 1/1,
2/3, 5/7, and
12/17. The theory of Diophantine approximation
produces the results:
di.
| is less than, or equal to,
1/di2.
The Farey approximation, as described by Plebani, is calculated by
subdividing an interval. The interval from a/b to
a'/b' is subdivided at
(a+a')/(b+b'); for the start interval [0,1]
this would, of course, be 1/2. You then check in which of the two newly
generated subintervals
lies. A median can now
be inserted into this interval and this process iterated until some
endpoint satisfies the desired condition.
The important facts about Farey series are as follows:
|
1/((n+1)v)
holds.
by a fraction with
denominator at most n.
It's possible to mix these two approaches, starting with a
continued-fraction estimate, then extending the result to the optimal
Farey approximation. To do this, I need to find the successor (or
predecessor) of
nl/dl
in the Farey series of order
dl. Start with the
complete quotient
nl/dl
and assume that
>
nl/dl (for the
other case, only the sign gcd(r,s) needs to be reversed). By
tracing the steps of the Euclidean algorithm, you can solve the equation
dlx-nly=gcd(r,s).
Now divide the whole equation by the gcd(r,s) and, henceforth,
assume the gcd to be one. If
(x0,y0)
is a solution, then
(x0+jnl,y0+jdl)
also is one for every integer j, so we can pick a j such
that
0<y0+jdl
nl and this j gives us the
successor of nl/dl in
the Farey series of order dl. Note that the
most computation-intensive part of this procedure--finding
(x0,y0)--amounts
to just finishing the Euclidean algorithm for r and s and
tracking the partial quotients. That is the algorithm used for computing
the complete quotients as well.
The larger the qi, the more interesting the
question of grouping these qi steps together
becomes. Given an arbitrarily chosen
, what are
the likely sizes of the qi? A thorough
discussion of this question entails delicate arithmetic estimates and an
examination of the underlying assumptions (see A.M. Rockett et al.,
"Continued Fractions," World Scientific, 1992).
A crude summary is that qi is "not bounded, but not too big too often either." In The Art of Computer Programming, Vol.2: Seminumerical Algorithms (Addison-|Wesley, 1969), Donald Knuth notes two interesting facts:
There are basically three different methods of calculating our desired approximation:
and check each one according to the desired
condition. This method is appropriate if your requirement is for
accuracy; the only possible loss is one additional complete
quotient.
, and the best possible
result is needed.
. In addition,
the algorithm is particularly simple.Graham R.L., D.E. Knuth, and O. Patashnik. Concrete Mathematics. AddisonWesley, 1989.
Hardy G.H. and E.M. Wright. An Introduction to the Theory of Numbers, fifth edition. Oxford University Press, 1979.
Knuth D.E. The Art of Computer Programming, Vol.2: Seminumerical Algorithms, Addison-Wesley, 1969.
Rockett A.M. and P. Szusz. "Continued Fractions," World Scientific, 1992.