fractions — 分数
优质
小牛编辑
140浏览
2023-12-01
创建有理数实例
# fractions_create_integers.py
import fractions
for n, d in [(1, 2), (2, 4), (3, 6)]:
f = fractions.Fraction(n, d)
print('{}/{} = {}'.format(n, d, f))
# fractions_create_strings.py
import fractions
for s in ['1/2', '2/4', '3/6']:
f = fractions.Fraction(s)
print('{} = {}'.format(s, f))
# fractions_create_strings_floats.py
import fractions
for s in ['0.5', '1.5', '2.0', '5e-1']:
f = fractions.Fraction(s)
print('{0:>4} = {1}'.format(s, f))
# fractions_from_float.py
import fractions
for v in [0.1, 0.5, 1.5, 2.0]:
print('{} = {}'.format(v, fractions.Fraction(v)))
# fractions_from_decimal.py
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)))
四则运算
# fractions_arithmetic.py
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))
近似值
# fractions_limit_denominator.py
import fractions
import math
print('PI =', math.pi)
f_pi = fractions.Fraction(str(math.pi))
print('No limit =', f_pi)
for i in [1, 6, 11, 60, 70, 90, 100]:
limited = f_pi.limit_denominator(i)
print('{0:8} = {1}'.format(i, limited))