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)))