Write a function that computes this expression, adding up terms until the absolute value of the next term is less than a specified tolerance tol
or until at most nmax
terms have been added.
I tried 'Import Decimal from Decimal' and float(c) but it did not work.
import math
def sin_taylor(x, tol=1e-7, nmax=100):
b=0
for i in range (nmax):
e = float(2*i+1)
c=float(math.factorial(e))
#print(c)
#print(b)
a=((((-1)**i))*(x**(e))/c)
b+=a
return b
When I assert sin_taylor(0)==0
, it gives 0 but when I
assert math.isclose(sin_taylor(math.pi/2),0.999999943741051)
, it gives a=((-1)**i*d)/c
OverflowError: int too large to convert to float