Using the fractions module of the standard Python library, you can perform calculations with fractions (rational numbers).
The following is explained here.
- Constructor of Fraction
- Get numerator and denominator values as integers
- Computing and comparing fractions (rational numbers)
- Converting fractions to decimals (float)
- Fraction to string (str) conversion
- Get rational number approximation
Constructor of Fraction
There are several ways to create a Fraction instance. In all cases, the fraction is automatically divided into fractions.
Specify numerator and denominator as integers
Specify the numerator and denominator as integers, respectively. If the denominator is omitted, it is assumed to be 1.
from fractions import Fraction print(Fraction(1, 3)) # 1/3 print(Fraction(2, 6)) # 1/3 print(Fraction(3)) # 3
decimal fraction (float)
If a fractional value is passed, it is converted to a fraction.
print(Fraction(0.25)) # 1/4 print(Fraction(0.33)) # 5944751508129055/18014398509481984
If you wish to approximate by specifying a maximum denominator, use the limit_denominator() method described below.
character string (str)
If a string value is passed, it is converted to a fraction.
print(Fraction('2/5')) # 2/5 print(Fraction('16/48')) # 1/3
Get numerator and denominator values as integers
Attributes of type Fraction allow you to obtain integer values for the numerator and denominator, respectively. They cannot be changed.
numerator
denominator
a = Fraction(1, 3) print(a) # 1/3 print(a.numerator) print(type(a.numerator)) # 1 # <class 'int'> print(a.denominator) print(type(a.denominator)) # 3 # <class 'int'> # a.numerator = 7 # AttributeError: can't set attribute
Computing and comparing fractions (rational numbers)
Arithmetic operators can be used to calculate addition, subtraction, etc.
result = Fraction(1, 6) ** 2 + Fraction(1, 3) / Fraction(1, 2) print(result) print(type(result)) # 25/36 # <class 'fractions.Fraction'>
Comparison operators can also be used.
print(Fraction(7, 13) > Fraction(8, 15)) # True
Converting fractions to decimals (float)
Can convert from fractions to decimals with float().
a_f = float(a) print(a_f) print(type(a_f)) # 0.3333333333333333 # <class 'float'>
When calculated with a decimal number, it is automatically converted to a float type.
b = a + 0.1 print(b) print(type(b)) # 0.43333333333333335 # <class 'float'>
Fraction to string (str) conversion
To convert to a string, use str().
a_s = str(a) print(a_s) print(type(a_s)) # 1/3 # <class 'str'>
Get rational number approximation
A rational number approximation can be obtained with the method limit_denominator() of type Fraction.
Returns the rational number (fraction) whose denominator is less than or equal to the argument max_denominator. If omitted, max_denominator=1000000.
Approximate irrational numbers such as pi and Napier number e
pi = Fraction(3.14159265359) print(pi) # 3537118876014453/1125899906842624 print(pi.limit_denominator(10)) print(pi.limit_denominator(100)) print(pi.limit_denominator(1000)) # 22/7 # 311/99 # 355/113 e = Fraction(2.71828182846) print(e) # 6121026514870223/2251799813685248 print(e.limit_denominator(10)) print(e.limit_denominator(100)) print(e.limit_denominator(1000)) # 19/7 # 193/71 # 1457/536
Convert circular decimals to fractions
a = Fraction(0.565656565656) print(a) # 636872674577009/1125899906842624 print(a.limit_denominator()) # 56/99 a = Fraction(0.3333) print(a) # 6004199023210345/18014398509481984 print(a.limit_denominator()) print(a.limit_denominator(100)) # 3333/10000 # 1/3