Rational2

Convert float to fractions
from fractions import Fraction import sys d = float(sys.argv[1]) f=d, Fraction.from_float(d).limit_denominator(1000) a=f[1] b=str(a) c=eval(b) print ("Decimal=",d) print ("Fraction:",a,"=",c) print ("Error=",c-d)
 * 1) !/usr/bin/python3



Approximate pi from increasingly better fractions
from fractions import Fraction import sys import numpy as np x = np.pi d = 10 while d < 1000000000: f=x, Fraction.from_float(x).limit_denominator(d) a=f[1] b=str(a) c=eval(b) print ("Denom. limit=",d) print (a,"=",c) d = d * 10
 * 1) !/usr/bin/python3