问题
Assuming I have a program with the function
def fakultaet(x):
if x>1:
return(x* fakultaet(x-1))
else:
return(1)
that returns the factorial of a given number, I need to calculate
1.0/fakultaet(200)
but I get an overflow error: long int too large to convert to float
.
How can I solve this problem?
回答1:
You could try this:
from decimal import Decimal
def fakultaet(x): # as you have it currently
if x>1:
return(x * fakultaet(x-1))
else:
return(1)
print Decimal(1.0) / fakultaet(200)
Output:
1.267976953480962421753016371E-375
Oh, and also, there is a factorial
function in the math
module already, just include from math import factorial
at the top of your file to obtain access to it.
来源:https://stackoverflow.com/questions/13316045/long-int-too-large-to-convert-to-float