home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky sci.math:17870 sci.math.num-analysis:3763
- Path: sparky!uunet!psinntp!kepler1!andrew
- From: andrew@rentec.com (Andrew Mullhaupt)
- Newsgroups: sci.math,sci.math.num-analysis
- Subject: Re: Approximating decimal fractions with proper fractions
- Keywords: fraction, approximation, number theory
- Message-ID: <1450@kepler1.rentec.com>
- Date: 8 Jan 93 22:40:51 GMT
- References: <C0ICCG.1BJ@hsi.com>
- Followup-To: sci.math
- Distribution: na
- Organization: Renaissance Technologies Corp., Setauket, NY.
- Lines: 77
-
- In article <C0ICCG.1BJ@hsi.com> rogerh@code3 (Roger Harrison) writes:
- >I am looking for an algorithm (preferably a computationally cheap one)
- >to approximate a decimal fraction with a proper fraction for display
- >purposes. The ideal algorithm would take a decimal fraction and the
- >maximum number of digits in the result's denominator to determine the
- >proper fraction (in numerator/denominator form) which best approximates
- >the original decimal fraction.
-
-
- I don't know what number theory books you looked in but any decent one
- should have done. What you are essentially looking for is the continued
- fraction, and it is usually computed by the Gauss transformation. You
- will find a discussion of this in Hardy and Wright's _Intro to Number Theory_
- and most other such texts, so I'll just sketch an example.
-
-
- Any real number x between 0 and 1 generates a 'continued fraction' of the
- following kind:
-
- x_0 = x;
-
- x_n-1 = (a_n + x_n)^-1; a_n positive integer, 0<x_n<1, for n>0.
-
- If you do this with the fractional part of Euler's number (e-2) you get
- the sequence:
-
- a_1 = 1, a_2 = 2, a_3 = 1, a_4 = 1, a_5 = 4, ...
-
- to make the pattern clear, you find that a_2 = 2, a_5 = 4, a_8 = 6, a_11 = 10,
- (i.e. a_(3k-1) = 2k) and a_n = 1 for n <> 3k-1.
-
- You will have to be careful to evaluate e to lots of digits if you want to
- see the pattern clearly. The relevance of this sequence to your question is
- that we have the representation for e:
-
- e = 2 + 1/(1 + 1/(2 + 1/(1 + 1/(1 + 1/(4 + 1/(1 + ... ))))))
-
- called a continued fraction. In the general case we write
-
- x = 1/(a_1 + 1/(a_2 + 1/(a_3 + ... )))
-
- which is often abbreviated as:
-
- x = [a_1, a_2, a_3, ... ]
-
- in our particular case:
-
- e = 2 + [1,2,1,1,4,1,1,6,1,1,8,1,1,10,1,1,12,1,...]
-
-
- Now if you start with a rational x, the continued fraction will terminate,
- and it is pretty clear that a terminating continued fraction must be rational.
- What isn't obvious, but the number theory books will spell out for you is
- that if you truncate a continued fraction, you get a rational approximation
- to x which is the best in a well defined sense. By successive truncations
- you get a sequence of approximating rationals, for example:
-
- e_1 = 2 + [1] = 3
-
- e_2 = 2 + [1,2] = 8/3 ~ 2.66667
-
- e_3 = 2 + [1,2,1] = 11/4 = 2.75
-
- e_4 = 2 + [1,2,1,1] = 19/7 ~ 2.714285
-
- e_5 = 2 + [1,2,1,1,4] = 87/32 ~ 2.71875
-
- and these are clearly tending to e = 2.7182818....
-
- So in order to find a good (in some sense best) approximation to a decimal
- number, work out the continued fraction and truncate it.
-
- This is also the way to work out things like the minimum number of at bats
- you need to have a .327 batting average, etc.
-
- Later,
- Andrew Mullhaupt
-