Python: division error
Heres my method:
def geometricSum(commonRatio, firstTerm, lastTerm):
return ((firstTerm - lastTerm) * commonRatio) / (1 - commonRatio)
Interpreter testing:
>>> geometricSum(1.0,1.0,100.0)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "F:\PortablePython_1.1_py3.0.1\sumOfGeometric.py", line 2, in geometricSum
return ((firstTerm-lastTerm)*commonRatio)/(1-commonRatio)
Zero开发者_C百科DivisionError: float division
You're dividing by zero. commonRatio is 1.0 so the denominator is 1-1.0 = 0.
Haha man, I'm sorry to be blunt, but this has nothing to do with doubles. You are DIVIDING BY ZERO! You could have blown up the universe if it wasn't for python's exception handling!
1 - commonRatio = 1 - 1 = 0.
Division by 0 is not yet supported in python. or in math.
Python told you what the problem is:
ZeroDivisionError: float division
Please read the error messages. It says ZeroDivisionError
because there was an Error
trying to perform a Division
by Zero
. You are dividing by 1 - commonRatio
, which is zero at that point in the program.
Looks like your commonRatio was 1 and so you were summing a divergent series.
with those parameters you are trying to divide by zero...
commonRatio = 1.0
/ (1-commonRatio)
/ 0
this throws a ZeroDivisionError exception
精彩评论