Javatpoint Logo
Javatpoint Logo

Fraction Module in Python

Python Fraction module is used to work with the arithmetic of rational numbers. This module allows us to create a Fractional instance of integers, floats, numbers, decimal, and strings.

What is a Fractional Instance?

Fractional instances can be created using the pair of integers, or from a string, or another rational number. These are hashable and should be treated as mutable.

How to create a fractional instance?

Fraction module provides the following built-in functions that are used to create fraction. Let's understand the following ways of creating fraction instance.

  • class fractions.Function(numerator = 0, denominator = 1): - This methods assigns numerator as 0 and denominator as 1 by default. If the denominator is equal to 0, it will raise the Zerodivision error. Let's understand the following example.

Example -

Output:

10/37
11/18
0
  • class fractions.Fraction(other_fraction) : This method takes the other_fraction instance of a number. It returns rational and a fraction instance with same value.
  • class fractions.Fraction(float) - This method takes the float value and returns the same value fraction instance.

Example -

Output:

6004799503160661/72057594037927936
  • class fractions.Fraction(decimal) :- This method takes the decimal instance and returns the instance with the same value. Let's understand the following example.

Example -

Output:

113/50
  • class fractions.Fraction(string) : - This method takes the string or Unicode as an argument and returns the instance with the same value. Let's understand the following example.

Example -

Output:

7/25
123/100
3/5
1414213/1000000
  • limit_denominator(max_denominator=1000000) - It is used to find the approximation to given floating-point number. It returns the closet Fraction to self that has denominator at max_denominator. Let's understand the following example.

Example -

Output:

157079632679489661923/50000000000000000000
355/113
311/99
22/7
5
2

Example - 2: Perform Mathematical Operation on Fractional

Output:

404/75
25/12
32/45
3/2
2.4326050606703427

Conclusion

In this tutorial, we have discussed Fraction module and its few important methods. These methods can be used in the complex mathematical computation and machine learning project.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA