Now that I've learned about the existence of the Python built-in fractions package that handles fractions, let's take a quick look at how it behaves.
Use Python 3.8.6. For the rest, we'll just use the built-in fractions package.
First of all, you need to import the fractions package to handle it. In particular, the Fraction class is the main usage, so import that.
from fractions import Fraction
The Fraction class accepts a first argument, the numerator, and a second argument, the denominator.
f = Fraction(numerator=3, denominator=4)
If you try to display an instance of the Fraction class on an interactive shell or Jupyter, it will be displayed as a repr, including the values of the numerator and denominator.
>>> f Fraction(3, 4)
If you look through the print function, it will be displayed as
>>> print(f) 3/4
It seems that Fraction instances can be compared with ints and floats as they are.
Comparison with int
>>> f = Fraction(numerator=4, denominator=4) >>> f == 1 True
Comparison with float
>>> f = Fraction(numerator=3, denominator=4) >>> f == 3 / 4 True
If you perform four arithmetic operations on a Fraction instance with int etc., the Fraction instance will be returned as it is.
>>> Fraction(3, 4) + 1 Fraction(7, 4)
>>> Fraction(5, 4) - 1 Fraction(1, 4)
>>> Fraction(3, 4) * 2 Fraction(3, 2)
If you add Fractions together, the result will also be an instance of Fractions.
>>> Fraction(1, 4) + Fraction(1, 4) Fraction(1, 2)
Fraction + Fraction + int will return Fraction as it is.
>>> Fraction(1, 4) + Fraction(1, 4) + 1 Fraction(3, 2)
If you perform four arithmetic operations with the values of Fraction and float, the result will be float.
>>> Fraction(3, 4) + 1.5 2.25
By specifying float as the argument of the numerator (numerator) of the first argument, you can create a Fraction instance even if you omit the denominator argument of the denominator.
>>> Fraction(numerator=0.25) Fraction(1, 4)
>>> Fraction(numerator=0.26) Fraction(1170935903116329, 4503599627370496)
>>> 1170935903116329 / 4503599627370496 0.26
You can also specify a character string using a half-width slash, such as
1/4, in the numerator argument.
>>> Fraction(numerator='1/4') Fraction(1, 4)
If you can reduce the amount when you create an instance of Fraction or perform a calculation, the reduction will be executed automatically.
>>> Fraction(2, 4) Fraction(1, 2)
>>> Fraction(1, 6) + Fraction(1, 6) Fraction(1, 3)
If you specify an irrational number, you can specify the maximum denominator value (max_denominator argument) to get an approximate value within the range of that value or less.
>>> Fraction(3.14159265359) Fraction(3537118876014453, 1125899906842624)
Setting the denominator is limited to 10 or less
>>> Fraction(3.14159265359).limit_denominator(max_denominator=10) Fraction(22, 7) >>> 22 / 7 3.142857142857143
Setting the denominator is limited to 150 or less
>>> Fraction(3.14159265359).limit_denominator(max_denominator=150) Fraction(3.14159265359).limit_denominator(max_denominator=150) >>> 355 / 113 3.1415929203539825