【发布时间】:2020-02-03 18:52:12
【问题描述】:
编写一个计算此表达式的函数,将各项相加,直到下一项的绝对值小于指定的容差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
【问题讨论】:
-
查看this answer 的类似问题。它可能会有所帮助。
标签: python taylor-series