Not all best rational approximations are the convergents of the continued fraction!

[For more background on continued fractions and why they are so wonderful at approximations (and wonderful generally) — eventually I may edit this post to mention that. For now I just want to quickly clarify something, which surprisingly many popular expositions of continued fractions seem to mislead by leaving out.]

Any real number can be written as a continued fraction. For instance, the square root of 3 is
written as ,

Rational numbers have finite continued fractions, and irrational numbers have infinite continued fractions, of which only quadratic irrationals (like √3 above) have periodic continued fractions. Non-quadratic irrationals may still have some recognisable patterns in their representation, like e above, or not, like π.

The numbers you get by truncating the continued fraction — taking only the first few terms — are called its convergents. The continued fraction for π is [3; 7, 15, 1, 292, 1, 1, 1, 2, 1, 3, 1, 14, 2…] (sequence A001203 in OEIS), and the sequence of convergents is 3/1, 22/7, 333/106, 355/113, 103993/33102… (A002485/A002486):

Each of these convergents is a best rational approximation, in the following sense.

Definition: A fraction p/q is a best rational approximation of a real number x if p/q is closer to x than any other fraction with a smaller denominator. That is, for any other fraction with and , we have .

Thus among all fractions with denominator at most 7, the fraction 22/7 is closest to π. Among all fractions with denominator at most 113, the fraction 355/113 is closest to π. And so on.

Every convergent is a best rational approximation, but these aren’t all of the best rational approximations! The sequence of best approximations of π is actually 3/1, 13/4, 16/5, 19/6, 22/7, 179/57, 201/64, 223/71, 245/78, 267/85, 289/92, 311/99, 333/106, 355/113, 52163/16604… (A063674/A063673), where only the bold terms are convergents. (Aside: Notice how good 355/113 is: it is the best among all fractions with denominator at most 16603. This is because of the large term 292 in the continued fraction of π.) The others are semi-convergents (intermediate fractions), as explained below.

In general, for any real number, the convergents of its continued fraction are always best rational approximations, but they aren’t all of the best rational approximations. To get all of them, you also have to take the semi-convergents: fractions of the form for some integer n≥1. Specifically, taking — the next term in the continued fraction expansion — gives , and the ones to take are precisely from (if it’s better than the previous convergent, else ) to . This is also mentioned on Wikipedia.

The above is the natural meaning of “best rational approximation”. This is also known as “best rational approximation of the first kind”. There is a different definition of “best rational approximation” under which the convergents are precisely all the best rational approximations: basically, instead of considering the distance , we consider . That is: A fraction is a best approximation of the second kind of a real number if for every fraction with and , we have
This measure turns out to have a nicer theory, but to omit specifying that this non-obvious definition is the one being used is misleading.

Program

For illustration, here’s a C program (why it’s written in C is not important right now) that given a positive real number, generates its continued fraction, its convergents, and the sequence of best rational approximations. The function find_cf finds the continued fraction (putting the terms in a[] and the convergents in p[] and q[] — excuse the global variables), and the function all_best prints all the best rational approximations.

All these terms are correct, though if you increase MAX you start getting errors because of precision. I’m myself impressed with how many terms you get with only 13 convergents. (As you can see, there’s a small bug where it sometimes doesn’t print the very first “n/1” approximation, or prints it incorrectly — I leave it for you to fix!)

Related

25 Responses

There’s something wrong with your algorithm for generating best rational approximations. It shows up, for example, in the output of the program when you give it the square root of two. Leaving aside the fact that the first result in the list of best rational approximations (2/1) is wrong, when we come to 10/7 there is an error, because 10/7 is further from sqrt(2) than 7/5 is. I believe that the problem is that you have not implemented the test to see whether a_k/2 is admissible as a result when a_k is divisible by 2. This test is described in the wikipedia article and amounts to comparing two continued fractions.

The formulation of rule 3 at on Wikipedia and in Grafic Gems V seems unnecessary complicated.
You just have to keep track of the differences
d_k=|p_k/q_k-x| for admissible fractions,
and scrap those pn/qn, which not give an improvement,
i.e. for which the latest d_k<=|p_n/q_n-x|.
Allan

I think you’re right (for some meaning of “think” as applies at 3am!). That is definitely much simpler.

But a possible issue with that is the precision to which |p_k/q_k – x| can be computed, if we don’t have x in a convenient form. So I think their goal is to give the rule in terms of just the continued fraction.
Meanwhile, the linked reference in Wikipedia, Graphics Gems 5 does give a simpler test on page 28 (the next page), if you generate the continued fraction using the a_k’s they use. The reference given is Concrete Mathematics, equations 6.131 and 6.135, which I plan to look at sometime when I’m more awake.

Thanks for your comment!

[2011-11-04: This is now fixed. The rule in terms of continued fractions is exactly equivalent to comparing the distance of x from the two, so what I said was stupid. — S.]

Thank you for pointing this out.
These precision problems is of course serious with the old compilers from 1980-2000;
but seem obsolate to day with Mathematica and Maple.
The condition in GEM V: Allow c_k/2 whenever d_(k-2)*a_(k-1)>d_(k-1)*a_k
is correct; but the a_i are only explained for rationals.
The refrences in Concrete Mathematics are only about some basic properties.
Allan

What do you mean by higher dimensions? If you mean multidimensional continued fractions, I know nothing about them, sorry.

I’m trying to find better references; do inform me if you find one. Meanwhile, the best I can find (and to which all other references seem to lead) is the classic book Continued Fractions by A. Khinchin. He defines the thing we want:Definition: A fraction is a best approximation of the first kind of a real number if for every fraction with and , we have
And he proves that every best approximation is either a convergent on an intermediate fraction (semiconvergent).

(A fraction is a best approximation of the second kind if for all such , and these are precisely the convergents.)

But surprisingly, he doesn’t characterize which semiconvergents (intermediate fractions) to take.

Thank you for refering from Thill’s paper, which not seems to have free access.
Is it about a numerical precision problem, or somthing about rational approximation?
On wiki seems needed that a_k should be even.
Allan

I have emailed the paper to you. The paper is about something else, but it quotes the above theorem (which is what we want), as background, citing Perron’s book. I don’t have access to the 1954 version, but in the 1913 edition available online, see section 2.16 starting here on page 55. On page 60 is the form (ii) of the rule in terms of continued fractions.

Here also we need that a_k is even as on Wikipedia (just now I renamed “n” to “k” in the previous comment), because the test only becomes necessary when 2i =a_k. When a_k is odd, the condition is automatically satisfied and doesn’t need to be tested (though testing it does no harm).

On wiki under the headline : Best rational approximations.
The “half rule” is that the ….
could perhaps be changed to
If a_k is even, the “half rule” is that the ….

Example: a=11/16=[0,1,2,5]
b=[0,1,2,3]=7/10 OK with b-a=1/80=0.0125…
c=[0,1,2,5/2]=12/17 not OK, as c-a=5/272=0.0183…,
because b is better approximation than c, and with
smaller denominator.
Thus c is not a a best approximation of first kind
Allan

As for Wikipedia, it was already mentioned earlier (the third item in the list) that the rule is to be checked only when ak is even. (When ak is odd, ak/2 is not an integer, so the rule doesn’t make sense for simple continued fractions anyway.) Anyway, I’ve re-edited the Wikipedia article to clarify as you suggested.

Thanks for the C code — it’s been very helpful! If you’re still maintaining this page, can you elaborate on your comment (“Enough to test when a[i+1] is even, actually”)? It doesn’t appear to be correct.

I think it’s correct: as mentioned in the text, the smallest to pick is either or , and when a[i+1] is odd, both of these are identical and more than (what C computes as) a[i+1]/2, which can be skipped.

I noticed when trying the example from Wikipedia (0.843785) that it misses several semiconvergents. The code in your post returns 0/1, 1/1, 5/6, 11/13, and 27/32 correctly, but it seems that it should also find 3/4, 4/5, and 16/19. (Ken Shoemake’s implementation from Graphics Gems V returns these as well.)

I can’t make heads or tails out of the notation in the Wikipedia article, so I’m not sure what’s wrong.

(The last line is the list of all best rational approximations; the formatted block above it is just the list of convergents — look at the examples in the post, and the description above the program. I guess I ought to have printed some text in the output, to make the output more clear and self-contained.)

Ah, my bad, I see where I went wrong — I thought the last line was a list of suboptimal or unnecessary results, so I didn’t even include that part of the code in my test. Makes sense now. :) Thanks again!

Thanks! It’s an interesting paper, but I’m not immediately able to figure out how the main theorem of the paper differs from the formulation known from Perron 1913, and mentioned on Wikipedia — isn’t it basically the same? (Of course, given the difficulty I had tracking down references, it’s always a great service to rediscover/republish the known results, given how ignored the question seems to be…)

I can no longer recall the specifics, but yes, more seems to have been known when we submitted this paper than we knew at the time. Perhaps more than Perron, I think Ken Shoemake’s book chapter cited in Wikipedia seems to have anticipated the bulk of our main theorem. Perhaps the characterisation in terms of partial quotients as to which of the two conditions holds is new, but I really don’t know. I was a second year undergrad when I came up with this, and was quite thrilled at the time:)

It is indeed an excellent result for anyone, let alone a second-year undergrad. :-) Very impressive. Thanks for pointing out your paper, being a self-contained result it’s easier to read than book sections.