Fraction Module in Python



In Python, the Fraction module supports rational number arithmetic. Using this module, we can create fractions from integers, floats, decimals, and other numeric values and strings.

The constructor of this class accepts Numerator and Denominator as parameters and creates Fractions from them. The default value of the numerator is 0 and the default value of the denominator is 1. The cost raises ZeroDivisionError when the denominator is 0.

Creating Fraction Instances

At first, we will see how the class can create fractions using Numerator and Denominator.

Example

from fractions import Fraction as frac
print(frac(45, 54))
print(frac(12, 47))
print(frac(0, 15))

Output

5/6
12/47
0

Handling Floating Point Numbers

We can provide some floating-point numbers as an argument for the Fraction object. If we provide the exact floating-point value, it will try to convert it to the numerator and denominator value of the integer type.

Example

from fractions import Fraction as frac
print(frac(33.33))
print(frac('33.33'))

Output

2345390243441541/70368744177664
3333/100

Using Strings with Fraction

Let us see, some other examples of string-type arguments to the Fraction object. It also supports the sign of the numbers. It supports the + or - sign.

Example

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

Limiting the Denominator

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().

Example

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

Mathematical Operations with Fractions

Fractions can also support mathematical operations, like addition, subtraction, multiplication, division, power, etc.

Example

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

Square Root, Floor, and Ceiling

The square root, floor, ceiling, and some other operations are also supported by this object.

Example

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
Updated on: 2024-10-09T11:17:29+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements