CORDIC算法是一种用于计算三角函数、反三角函数和其他一些数学函数的迭代算法。CORDIC代表“Coordinate Rotation Digital Computer”,最初设计用于计算机数字化处理中的旋转操作,但后来被扩展用于更广泛的数学运算。该算法通过一系列迭代旋转和移位操作来逼近给定角度的三角函数值。
Coordinate Rotation Dlgital Computer(CORDIC)算法
参考:Computer Arithmetic: Algorithm and Hardware Design
defcordic(value, mode): z = 0 if(mode==0): x = 0.607252935 y = 0.0 if(mode==1): x = 1 y = value for i inrange(13): if(mode==0): d = np.sign(value) else: d = -np.sign(y)
if(d==0):d = 1 value -= d*lookuptable[i]
z -= d*lookuptable[i]
temp = x x -= d * y * 2.0**(-i) y += d* temp * 2.0**(-i) # print("y:",y) # print("x:",x)
计算范围为: With these provisions, convergence in computing hyperbolic sine and cosine functions is guaranteed for |z| < 1.13 and in the case of the tanh−1 function, for |y| < 0.81.
import numpy as np import math import matplotlib.pyplot as plt
for i inrange(1,20): lookuptable_tanh[i] = math.atanh(2**(-i)) print(math.atanh(2**(-i)))
defmy_tanh(value): k=0.8281593609602 x=1/k y=0 for i inrange(1,20): if(value)>0: d=1 else: d=-1 value = value - d*lookuptable_tanh[i] temp=x x = x + d*y*2**-i y = y + d*temp*2**-i
# if(i==4 or i==13): # if(value)>0: # d=1 # else: # d=-1 # value = value - d*lookuptable_tanh[i] # temp=x # x = x + d*y*2**-i # y = y + d*temp*2**-i print("my_cosh:",x) print("my_sinh:",y) print("my_tanh:",y/x)