如果我使用某种算法来查找指数近似和,我试图了解有多少 FLOP,特别是如果我在 python 中使用 math.factorial(n) 。我了解二元运算的 FLOP,那么阶乘也是函数中的二元运算吗?不是计算机科学专业的,我对这些有一些困难。我的代码如下所示:
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
import math
x = input ("please enter a number for which you want to run the exponential summation e^{x}")
N = input ("Please enter an integer before which term you want to turncate your summation")
function= math.exp(x)
exp_sum = 0.0
abs_err = 0.0
rel_err = 0.0
for n in range (0, N):
factorial = math.factorial(n) #How many FLOPs here?
power = x**n # calculates N-1 times
nth_term = power/factorial #calculates N-1 times
exp_sum = exp_sum + nth_term #calculates N-1 times
abs_err = abs(function - exp_sum)
rel_err = abs(abs_err)/abs(function)
请帮助我理解这一点。我也可能对其他 FLOP 有误!