Best rational approximations of a float


This snippet generates the best rational approximations of a floating point number, using the method of continued fractions (tested with python 2.6 and 3.1).

About the Author

Mind explorer.

#!/usr/bin/env python

"""A generator of the best rational approximations of a float
   using continued fractions.

   possible improvements left to the interested reader:
      - extend the generator to support decimal.Decimal numbers.
      - extend the generator to support gmpy.mpf numbers.

def best_rationals(afloat):
  """generate triples (num, den, error) where num/den
  is a best rational approximation of the float afloat and
  error is the difference (afloat - num/den)"""
  afloat, lastnum, num = ((-afloat, -1, int(-afloat)) 
                         if afloat < 0 else (afloat, 1, int(afloat)))
  lastden, den = 0, 1
  rest, quot = afloat, int(afloat)
  while True:
    yield num, den, afloat - float(num)/den
    rest = 1.0/(rest - quot)
    quot = int(rest)
    lastnum, num, lastden, den = (num, quot * num + lastnum,
                                  den, quot * den + lastden)

def testit():
    """Example use of best_rationals()"""
    from math import pi
    value = (pi ** 2) / 6
    gen = best_rationals(value)
    for i in range(20):
        num, den, err = next(gen)
        print ("{0} {1} {2:.2e}" .format(num, den, abs(err)))

if __name__ == "__main__":
Gribouillis 1,391 Programming Explorer Team Colleague

Note that starting with python 2.6, you can use the fractions module, which gives similar results

from math import pi
value = (pi ** 2) / 6
from fractions import Fraction
"""my output --->
Gribouillis 1,391 Programming Explorer Team Colleague

If you want approximations for very big numbers, you can use the clnum big numbers library. Here is an exemple showing the square root of 2 with 1000 digits and 2 rational approximations computed with clnum

>>> from clnum import *
>>> s = mpf("2.0", 1000)
>>> v = sqrt(s)
>>> v
>>> ratapx(v, 10000000000000000000000000)
>>> ratapx(v, 1000000000000000000000000000000000000000000000000)
TrustyTony 888 pyMod Team Colleague Featured Poster

On the other hand useful fact is also to remember that

>>> print(Fraction.from_float(pi).limit_denominator(10000))
>>> 355/113.0
>>> 355/113.0-pi

Gribouillis 1,391 Programming Explorer Team Colleague

A nice library to consider is the bigfloat module, which is a python wrapper around the well known mpfr C library based on GNU gmp. Install it from pypi ( easy_install bigfloat ). Then

>>> import bigfloat as bf
>>> with bf.Context(precision=1000):
...  x = bf.BigFloat("2")
...  root = bf.sqrt(x)
...  print root.as_integer_ratio()
(473544376400726394228831039451913480123688799751094630616578363333661533455686216226143010758906440540743599383075282477937325116004184859362870439857750473316274124854227871716121348224479943162567953976022762080355598781683844459583873884188875089875732987543720916348829425487472888348035796043929L, 334846439745708537796382827831250565800439003657979252326171996365734703476542538279124493379904955664873335286735358382870982901778848138624518049209330462622955242963257218294408581408199098183686068192282702343236935664606211486223923248314908216080349889927704442739388432239144512088662677127168L)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.