Webb
Webb

Reputation: 139

Fractions with python

I'm trying to do this calculation:

from fractions import Fraction 
z=4
x=Float(Fraction(1+math.pi,1+2*z**2))

But this results in the following error:

TypeError: both arguments should be Rational instances

If I change pi value for integer value works. But if I use a decimal value shows me that error.

Any idea?

Regards Thanks

Upvotes: 0

Views: 333

Answers (1)

Brian61354270
Brian61354270

Reputation: 14484

Taken from the documented for the fractions module:

The [two argument constructor] requires that numerator and denominator are instances of numbers.Rational.

In your snippet, 1+math.pi is a float, not an instance of numbers.Rational, hence the TypeError.

Upvotes: 1

Related Questions