Best rational approximations is a technical term and not subjective. The best rational approximations are the convergents of a value plus it's semi convergents that are closer to the original value than the previous convergents.
Hi everyone,
I've written some quick code in Python that is my tool for exploring various aspects of music theory, number theory, and my obsession with OEIS. It's currently written to spit out a list of the ratio's themselves. It would be quite easy to tweak to your needs. This may also help anyone exploring the logic of infinite continued fractions and convergents. I use the decimal module here for precision. It's written so you can paste any value you want into the input field - that can easily be tweaked to accept any real number from code. Phasers to stun. I call it my approximilator.
(Python 3)
import decimal
import math
D = decimal.Decimal
def contFrac(x, k):
"""Cont Frac from Real value"""
cf = []
q = math.floor(x)
cf.append(q)
x = x - q
i = 0
while x != 0 and i < k:
q = math.floor(1 / x)
if q > k:
break
cf.append(q)
x = 1/x - q
i += 1
return cf
def bestra(clist, app):
"""Best Rational Approximation from Cont. Frac"""
hn0, kn0 = 0, 1
hn1, kn1 = 1, 0
"""Best Rational Approx Init"""
ran, rad = 0, 0
conlist, ralist, finallist = [], [], []
for n in clist:
for i in range(1, n+1):
ran = (hn0 + (i*hn1))
rad = (kn0 + (i*kn1))
try:
if D.copy_abs(app-D(ran/rad)) < D.copy_abs(app-D(hn1/kn1)):
ralist.append({'ratio': f'{ran}/{rad}', 'denom' : rad})
except:
pass
hn2 = (n*hn1)+hn0
kn2 = (n*kn1)+kn0
conlist.append({'ratio': f'{hn2}/{kn2}', 'denom': kn2})
hn0, kn0 = hn1, kn1
hn1, kn1 = hn2, kn2
for x in sorted(conlist+ralist, key = lambda i: i['denom']):
finallist.append(x['ratio'])
return list(dict.fromkeys(finallist))
if __name__ == "__main__":
value = D(input('Input value to approximate: '))
prec = len(str(value))*2
decimal.getcontext().prec = prec
vc = contFrac(value, prec)
print(bestra(vc, value))
Some output for Pi to entice:
'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', '52518/16717', '52873/16830', '53228/16943', '53583/17056', '53938/17169', '54293/17282', '54648/17395', '55003/17508', '55358/17621', '55713/17734', '56068/17847', '56423/17960'