Python-Math-Algorithms/taylor_sine_approx/__init__.py

25 lines
469 B
Python
Raw Permalink Normal View History

2023-05-22 15:56:31 +00:00
import math
# iterative factorial function
def fac(x):
factor = 1.0
for i in range(1, x + 1):
factor *= i
return factor
# approximation of the sine function using taylor series
def taylor_sine(n, x):
k = 0.0
sign = -1.0
for i in range(0, n):
sign *= -1.0
k += sign / fac(2 * i + 1) * math.pow(x, 2 * i + 1)
return k
def test():
print("taylor: %s, sine: %s" % (taylor_sine(16, math.pi), math.sin(math.pi)))