def ex(x: Union[int, float]) -> float: """ Calculate value of e^x with Taylor series :param x: x value :return: e^x value """ e_x = 1 for i in range(1, 10): e_x += x**i / factorial_iterative(i) return e_x
def sinx(x: Union[int, float]) -> float: """ Calculate sin(x) with Taylor series :param x: x value :return: sin(x) value """ sin_x = x for n in range(1, 10): sin_x += (-1)**n * x**(2*n+1)/factorial_iterative(2*n+1) return sin_x
def sinx(x: Union[int, float]) -> float: """ Calculate sin(x) with Taylor series :param x: x value :return: sin(x) value """ if x == 0: return 1 counter = 0 res = x while counter < 50: counter += 1 if not counter % 2: e = (x**(2 * counter + 1)) / factorial_iterative(2 * counter + 1) res += e if counter % 2: e = (x**(2 * counter + 1)) / factorial_iterative(2 * counter + 1) res -= e return res
def ex(x: Union[int, float]) -> float: """ Calculate value of e^x with Taylor series :param x: x value :return: e^x value """ if x == 0: return 1 counter = 0 res = 1 while counter < 50: counter += 1 e = (x**counter) / factorial_iterative(counter) res += e return res
def test_fact_iterative(self): self.assertEqual(math.factorial(12), fact.factorial_iterative(12), msg="I think there's a mistake :)")
def test_fact_iter_exc(self): with self.assertRaises(ValueError, msg="ValueError should be here..."): fact.factorial_iterative(-1121)
def test_fact_iter_zero(self): self.assertEqual(1, fact.factorial_iterative(0), msg="I think there's a mistake :)")