Daniel La Cour
Daniel La Cour

Reputation: 23

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?

Upvotes: 2

Views: 138

Answers (1)

Dmitry Yantsen
Dmitry Yantsen

Reputation: 1185

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.

Upvotes: 2

Related Questions