In Python the Fraction module supports rational number arithmetic. Using this module, we can create fractions from integers, floats, decimal and from some other numeric values and strings.
There is a concept of Fraction Instance. It is formed by a pair of integers as numerator and denominator.
The class fractions.Fractionis used to create a Fraction object. It takes Numerator and Denominator. The default value of the numerator is 0 and denominator is 1. It raises ZeroDivisionError when the denominator is 0.
At first we will see how the class can create fractions using Numerator and Denominator.
Example code
from fractions import Fraction as frac print(frac(45, 54)) print(frac(12, 47)) print(frac(0, 15))
Output
5/6 12/47 0
We can provide some floating point numbers as an argument of the Fraction object. If we provide the exact floating point value, it will try to convert it to numerator and denominator value of integer type. In this case, it tries to reach to the approximate value. If the floating point number is provided as a string, it will try to find the exact value as Fraction. From the following examples, you can see the differences.
Example code
from fractions import Fraction as frac print(frac(33.33)) print(frac('33.33'))
Output
2345390243441541/70368744177664 3333/100
Let us see, some other examples on string type arguments to the Fraction object. It also supports the sign of the numbers. It supports + or - sign.
Example code
from fractions import Fraction as frac print(frac('5/6')) print(frac('-25.12')) print(frac('96.251 \t\n')) print(frac('3.14159265359'))
Output
5/6 -628/25 96251/1000 314159265359/100000000000
As we have seen, sometimes the denominators are very large in the Fraction object. So we can limit the denominator lengths. The default length is 1000000. It helps to perform rational approximation for floating point data. To limit the denominator, there is a function called limit_denominator().
Sometimes we want only the numerators or the denominators without the whole fraction object. So this method has numerator and denominator keyword to get them.
Example code
from fractions import Fraction as frac print(frac('3.14159265359')) print(frac('3.14159265359').limit_denominator(1000)) print(frac('3.14159265359').limit_denominator(100)) print(frac('3.14159265359').limit_denominator(10)) print(frac('36.25')) print(frac('36.25').numerator) print(frac('36.25').denominator)
Output
314159265359/100000000000 355/113 311/99 22/7 145/4 145 4
Fractions can also support the mathematical operations, like addition, subtraction, multiplication, division, power etc.
Example code
from fractions import Fraction as frac print('Add: ' + str(frac('5/4') + frac('9/8'))) print('Subtract: ' + str(frac('15/20') - frac('2/8'))) print('Multiply: ' + str(frac('2/3') * frac('5/7'))) print('Divide: ' + str(frac('80/125') / frac('12/45'))) print('Power: ' + str(frac('5/6') ** 3))
Output
Add: 19/8 Subtract: 1/2 Multiply: 10/21 Divide: 12/5 Power: 125/216
The square root, floor, ceiling and some other operations are also supported by this object.
Example code
from fractions import Fraction as frac import math print('Square Root: ' + str(math.sqrt(frac(36, 64)))) print('Square Root: ' + str(frac(math.sqrt(frac(36, 64))))) print('Floor Value: ' + str(math.floor(frac('22/7')))) print('Ceiling Value: ' + str(math.ceil(frac('22/7'))))
Output
Square Root: 0.75 Square Root: 3/4 Floor Value: 3 Ceiling Value: 4