my homework is to write a code which contains a function that calculates the sinx taylor series and gives the amount back. the function must get (n,k) which n is the requested number for sine,and k is the digits that function must calculate after the dot. first i neglected k since its easy to limit the numbers after the dot,and wrote a function that just calculates sinx taylor,so i gave it a specific range for r(r is every sentence of the taylor series):
def taylor(n,k):
s= ((math.pi)/180)*n
ex = s
sign = 1
factorial = 1
sum=0
i=1
r=1
while r>0.00000000000000000001 or r<0.0000000000000000000001 :
r= ex*sign/factorial
ex = ex*s*s
sign = sign*(-1)
factorial=factorial*(i+1)*(i+2)
i= i+2
sum = sum + r
return sum
import math
print(taylor(45,1))
i just don't know why if i set amount of r larger than this (i.e 0.1) i get this error:
Traceback (most recent call last):
File "/Users/modern/Desktop/taylor.py", line 22, in <module>
print(taylor(45))
File "/Users/modern/Desktop/taylor.py", line 12, in taylor
r= ex*sign/factorial
OverflowError: int too large to convert to float