泰勒級數求 sinx

Run Settings
LanguagePython
Language Version
Run Command
def fact(n): if n <= 1: return 1 else: return n * fact(n - 1) def taylor_sin(x, m): value = 0 eps = 1e-18 for i in range(1, m + 1): value += ((-1) ** (i - 1)) * ((x ** (2*i-1)) / fact(2*i-1)) if i == 1: last = value #print(i, last, value, abs(value-last)) else: #print("i=",i, "last=",last, "value=", value, "a-b=",abs(value-last)) if abs(value-last) < eps: return value else: last = value def taylor_cos(x, m): a = 1 count = 1 for k in range(1, m): if count%2 != 0: a -= (x**(2*k)) / fact(2*k) else: a += (x**(2*k)) / fact(2*k) count += 1 return a def sin(x): return taylor_sin(x, 300) def cos(x): return taylor_cos(x, 100) x = 1 #print("sin(",x ,")=", sin(x, 100)) print("sin(%d)= %.12f"%(x, sin(x) )) #print("cos(%d)= %.12f"%(x, cos(x) ))
Editor Settings
Theme
Key bindings
Full width
Lines