In [1]:
import fractions
for n, d in [(1,2),(2,4),(3,6)]:
f = fractions.Fraction(n,d)
print('{}/{}={}'.format(n,d,f))
In [2]:
import fractions
for s in ['1/2', '2/4', '3/6']:
f = fractions.Fraction(s)
print('{}={}'.format(s, f))
In [4]:
import fractions
for s in ['0.5', '1.5', '2.0', '5e-1']:
f = fractions.Fraction(s)
print('{0:>4}={1}'.format(s, f))
In [5]:
import fractions
for v in [0.1, 0.5, 1.5, 2.0]:
print('{}={}'.format(v, fractions.Fraction(v)))
In [6]:
import decimal
import fractions
values= [
decimal.Decimal('0.1'),
decimal.Decimal('0.5'),
decimal.Decimal('1.5'),
decimal.Decimal('2.0'),
]
for v in values:
print('{}={}'.format(v, fractions.Fraction(v)))
In [8]:
import fractions
f1 = fractions.Fraction(1,2)
f2 = fractions.Fraction(3,4)
print('{} + {} = {}'.format(f1, f2, f1 + f2))
print('{} - {} = {}'.format(f1, f2, f1 - f2))
print('{} * {} = {}'.format(f1, f2, f1 * f2))
print('{} / {} = {}'.format(f1, f2, f1 / f2))
In [9]:
import fractions
import math
print('PI =', math.pi)
f_pi = fractions.Fraction(str(math.pi))
print('No limit=', f_pi)
In [10]:
for i in [1,6,11,60,70,90,100]:
limited = f_pi.limit_denominator(i)
print('{0:8}={1}'.format(i, limited))