Daniel La Cour Daniel La Cour - 1 month ago 8
Python Question

Incorrect output from simplifying fraction

I have a simple code, written to simplify fractions in python. Putting in certain numbers (Such as shown in the code) give huge, and possibly incorrect values. Below is the code.

from fractions import Fraction
print(Fraction(36/40))


And it outputs:

8106479329266893/9007199254740992


Why does it do this, and how can I fix this?

Answer

I believe that you intended to use Fraction(36, 40). Notice the comma instead of the slash. What happens is that you input a division result instead of the numerator and denominator.

There are quite a few ways to init the Fraction. Take a look at the docs.