programming-examples/python/Math/Convert a floating point number to an approximate rational value.py

11 lines
287 B
Python
Raw Normal View History

2019-11-15 12:59:38 +01:00
import fractions
import math
print('PI =', math.pi)
f_pi = fractions.Fraction(str(math.pi))
print('No limit =', f_pi)
for d in [1, 5, 50, 90, 100, 500, 1000000]:
limited = f_pi.limit_denominator(d)
print('{0:8} = {1}'.format(d, limited))