Search code examples
pythonfractions

Determine the value of a Fraction is integer or float in python


I come across the Fraction module in python. I have problems to determine the result of an integer producting the fraction.

For example, the fraction is f = Fraction(1, 3) and when number n = 2 product with it, the result is Fraction(2, 3) = 0.666666666.... When n=3, the result shoude be Farction(3, 3)=1.

My question is how to distinguish Fraction(3, 3) with Fraction(1,3). That's to say the value of a fraction is integer or float? Thanks!


Solution

  • If f.denominator == 1, the number is an integer. Fraction automatically simplifies the number, so Fraction(3, 3) == Fraction(1, 1).