编写一个计算这个表达式的函数,将各项相加,直到下一项的绝对值小于指定的容差tol或直到最多nmax相加。
我尝试了“从十进制导入十进制”和 float(c) 但它不起作用。
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
当我断言sin_taylor(0)==0时,它给出 0 但是当我断言时math.isclose(sin_taylor(math.pi/2),0.999999943741051),它给出 a=((-1)**i*d)/c
OverflowError: int too large to convert to float
MYYA
森栏
相关分类