python - ZeroDivisionError: 0.0 to a negative or complex power -
i wanted find integral ((sin x)^8, {x,0,2*pi}) , tried write simple program, without external modules "math", calculate taylor series , summarize interval (0,2*pi) errors
traceback (most recent call last): file "e:\python\shad\sin.py", line 27, in <module> sum+=(ser(i*2*3.1415926/k))**8 file "e:\python\shad\sin.py", line 21, in ser sin_part+=((-1)**(j-1))*(a**(2j-1))/(fact(2*j-1)) zerodivisionerror: 0.0 negative or complex power
suddenly occurs. , don't see divided 0 or have power of complex number, variables have real positive value. "k" value both terms of series quantity , interval (0,2*pi) division.
sum=0 k=20 res=0 def fact(t): if t==0 or t==1: res=1 else: res=1 l in range(2,t+1): res=res*l return res def ser(a): sin_part=a j in range(2,k): print fact(2*j-1) sin_part+=((-1)**(j-1))*(a**(2j-1))/(fact(2*j-1)) print 'yay' return sin_part in range(0,k-1): sum+=(ser(i*2*3.1415926/k))**8 print sum
and don't see divided 0 or have power of complex number, variables have real positive value.
not true. on first iteration of
for in range(0,k-1): sum+=(ser(i*2*3.1415926/k))**8
you have i=0
, argument ser
0, a == 0
, , have (a**(2j-1))
, takes 0 complex power.
maybe meant a**(2*j-1)
? python uses j
unit imaginary, , 2j-1
complex number.
Comments
Post a Comment