如何写cos(1)

时间:2016-02-01 16:12:50

标签: python-3.x

我需要找到一种使用while循环在python中编写cos(1)的方法。但我不能使用任何数学函数。有人可以帮助我吗?

例如我还必须写exp(1)的值,我能够写下来:

count = 1

term = 1

expTotal = 0 

xx = 1 

while abs(term) > 1e-20:

    print("%1d  %22.17e" % (count, term))
    expTotal = expTotal + term
    term=term * xx/(count)
    count+=1

尽管如此,我仍然完全失去了如何使用cos和sin值。

2 个答案:

答案 0 :(得分:3)

只需更改表达式即可将术语计算为:

term = term * (-1 * x * x)/( (2*count) * ((2*count)-1) )   

将计数乘以2可以更改为将计数增加2,所以这是你的copypasta:

import math 

def cos(x):
    cosTotal  = 1
    count = 2
    term = 1
    x=float(x) 
    while abs(term) > 1e-20:
        term *= (-x * x)/( count * (count-1) )   
        cosTotal += term
        count += 2
        print("%1d  %22.17e" % (count, term))
    return cosTotal

print( cos(1) )
print( math.cos(1) )

答案 1 :(得分:1)

您可以使用此函数的泰勒展开式计算cos(1)

Taylor series for a cosine

您可以在Wikipedia上找到更多详细信息,请参阅以下实施内容:

import math

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

def cos(order):            
    a = 0
    for i in range(0, order):                
        a += ((-1)**i)/(factorial(2*i)*1.0) 


    return a

print cos(10)
print math.cos(1)

这给出了输出:

0.540302305868
0.540302305868

编辑:显然,使用CORDIC算法在硬件中实现余弦,该算法使用查找表来计算atan。请参阅下面基于此Google小组question的CORDIS算法的Python实现:

#atans = [math.atan(2.0**(-i)) for i in range(0,40)]
atans  =[0.7853981633974483, 0.4636476090008061, 0.24497866312686414, 0.12435499454676144, 0.06241880999595735, 0.031239833430268277, 0.015623728620476831, 0.007812341060101111, 0.0039062301319669718, 0.0019531225164788188, 0.0009765621895593195, 0.0004882812111948983, 0.00024414062014936177, 0.00012207031189367021, 6.103515617420877e-05, 3.0517578115526096e-05, 1.5258789061315762e-05, 7.62939453110197e-06, 3.814697265606496e-06, 1.907348632810187e-06, 9.536743164059608e-07, 4.7683715820308884e-07, 2.3841857910155797e-07, 1.1920928955078068e-07, 5.960464477539055e-08, 2.9802322387695303e-08, 1.4901161193847655e-08, 7.450580596923828e-09, 3.725290298461914e-09, 1.862645149230957e-09, 9.313225746154785e-10, 4.656612873077393e-10, 2.3283064365386963e-10, 1.1641532182693481e-10, 5.820766091346741e-11, 2.9103830456733704e-11, 1.4551915228366852e-11, 7.275957614183426e-12, 3.637978807091713e-12, 1.8189894035458565e-12]

def cosine_sine_cordic(beta,N=40):
    # in hardware, put this in a table.
    def K_vals(n):
        K = []
        acc = 1.0
        for i in range(0, n):
              acc = acc * (1.0/(1 + 2.0**(-2*i))**0.5)
            K.append(acc)
        return K
    #K = K_vals(N)
    K = 0.6072529350088812561694
    x = 1
    y = 0

    for i in range(0,N):
        d = 1.0
        if beta < 0:
            d = -1.0

        (x,y) = (x - (d*(2.0**(-i))*y), (d*(2.0**(-i))*x) + y)
        # in hardware put the atan values in a table
        beta = beta - (d*atans[i])
    return (K*x, K*y)

if __name__ == '__main__':
    beta = 1
    cos_val, sin_val = cosine_sine_cordic(beta)
    print "Actual cos: " + str(math.cos(beta))
    print "Cordic cos: " + str(cos_val)

这给出了输出:

Actual cos: 0.540302305868
Cordic cos: 0.540302305869