Find the numerator and denominator of a fraction in Python
Hello Geek! In this article, we will be learning how to find the numerator and denominator of a fraction in Python.
Before getting started let us learn a little about the fractions module.
fractions Module in Python helps us to perform arithmetic operations on rational numbers. We can create an object of the Fraction class by passing a pair of Integers, a String, or a Floating point number to the constructor.
We can import the Fraction class using the import statement below:
from fractions import Fraction
Creating a Fraction instance using a pair of Integers
Syntax: fractions.Fraction(numerator = 0, denominator = 1)
We can create a Fraction instance by passing a pair of integers to the constructor. The default values of numerator and denominator are set to 0 and 1 respectively.
Note: In the above syntax passing the denominator argument with 0 will result in ZeroDivisionError.
Note: The GCD of the numerator and denominator in the resultant Fraction instance will be 1.
Example: fractions.Fraction(2,4) will give an instance of Fraction(1,2).
Creating a Fraction instance using a String
We can use strings represented in the form of ‘p/q’ or ‘z’.
Here p and q are instances of numbers.Rational class, z can be a floating-point number or a scientific notation of a number. That is a string can be ‘1/2’ or ‘1e-3’ or ‘1.25’.
Creating a Fraction instance using a Decimal
We can also use a decimal object as an argument to the constructor to create a Fraction instance with an equivalent numerator and denominator.
Creating a Fraction instance using a float
Similar to a decimal object we can also pass a float value as a parameter. This will create a Fraction instance with the equivalent numerator and denominator values.
Note: fractions.Fraction(2.3) is not equal to fractions.Fraction(decimal.Decimal(‘2.3’)) due to the limitations of floating-point numbers that is the binary approximation of floating-point numbers.
Creating a Fraction instance using another Fraction instance
We can also pass another Fraction as an argument to the constructor of the Fraction class to create a Fraction instance from it.
Finding the numerator and denominator of a Fraction instance
Note that In all the above ways in creating a fraction, a Fraction instance with the equivalent numerator and denominator will be created.
Let f be a Fraction instance, then we can get its numerator and denominator as f.numerator and f.denominator.
Now, let us dive into the program to find the numerator and denominator of a fraction and also understand the concepts better.
Let us start with importing the Fraction class and the Decimal class from fractions and decimal modules respectively.
Now, let us create different Fraction instances using all the different ways that we discussed earlier.
Finally, we will print each fraction with its numerator and denominator.
from fractions import Fraction from decimal import Decimal f_int = Fraction(1,2) f_string_1 = Fraction('13/9') f_string_2 = Fraction('1e-3') f_string_3 = Fraction('1.25') f_decimal = Fraction(Decimal('2.3')) f_float = Fraction(2.3) f_fraction = Fraction(f_float) print("Fraction :",f_int,"| Numerator = ",f_int.numerator,"Denominator = ",f_int.denominator) print("Fraction :",f_string_1,"| Numerator = ",f_string_1.numerator,"Denominator = ",f_string_1.denominator) print("Fraction :",f_string_2,"| Numerator = ",f_string_2.numerator,"Denominator = ",f_string_2.denominator) print("Fraction :",f_string_3,"| Numerator = ",f_string_3.numerator,"Denominator = ",f_string_3.denominator) print("Fraction :",f_decimal,"| Numerator = ",f_decimal.numerator,"Denominator = ",f_decimal.denominator) print("Fraction :",f_float,"| Numerator = ",f_float.numerator,"Denominator = ",f_float.denominator) print("Fraction :",f_fraction,"| Numerator = ",f_fraction.numerator,"Denominator = ",f_fraction.denominator)
Fraction : 1/2 | Numerator = 1 Denominator = 2 Fraction : 13/9 | Numerator = 13 Denominator = 9 Fraction : 1/1000 | Numerator = 1 Denominator = 1000 Fraction : 5/4 | Numerator = 5 Denominator = 4 Fraction : 23/10 | Numerator = 23 Denominator = 10 Fraction : 2589569785738035/1125899906842624 | Numerator = 2589569785738035 Denominator = 1125899906842624 Fraction : 2589569785738035/1125899906842624 | Numerator = 2589569785738035 Denominator = 1125899906842624
From the above output, you can notice that f_float and f_decimal are not equal as we discussed earlier in this article.
That’s it! We have learned to create a fraction and also to fetch their numerator and denominator in Python.
Thank you for reading this article and I hope it helped you. In case of any doubts, feel free to post them below.
Also do check out our other related articles :