janmr blog

Good Rational Bounds

Say we have a k-bit integer and we want to allocate a character array to hold the decimal digits of this number. How large must the array be? Well, the exact formula is

but we are concerned about efficiency and don't want to use floating point calculations. So we fire up a calculator and get . This means that we can use, e.g., as an upper bound for and allocate characters. But we are also concerned about memory and think it is a waste to use little more than 10% more memory than needed. We could just use as an upper bound, but we don't want to use too large constants because we are also concerned about arithmetic overflow.

This is where continued fractions come in, see an earlier post for some basic notation and properties. Let where all are non-negative integers. Such a representation has some very desirable properties regarding rational approximations and bounds (the “truncated” continued fraction for is called the th convergent of ):

  1. The even convergents are always less than . Similarly, the odd convergents are always greater than .
  2. The th convergent is a very good rational approximation to . (We will not make this statement exact, but just say that a rational fraction is a best approximation if every other rational fraction with the same or smaller denominator differs from by a greater amount.)

We will now apply these facts to our example. We start by obtaining any rational upper bound with the restriction that it must be tighter than the good rational bound that we are aiming for. We have and we compute the continued fraction for this upper bound,

We now begin to compute the odd convergents, and keeping an eye on how tight they are:

  • (too much by about 11%)
  • (too much by about 0.015%)
  • (too much by about 0.00031%)

There are more odd convergents to consider, but we probably don't need to go any further for this use case. So we can allocate characters, knowing that it is a very tight upper bound.

(Note how a similar approach could be used if we were looking for a lower bound instead.)