Last active
June 17, 2023 15:15
-
-
Save smarteist/8515b9ff3da3fa504840b6ff82a6c432 to your computer and use it in GitHub Desktop.
CORDIC Algorithm to calc natural logarithm in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
def cordic_ln(x): | |
if x <= 0: | |
raise ValueError("Input must be greater than 0") | |
lut = [ | |
2.718281828459045, | |
1.6487212707001282, | |
1.2840254166877414, | |
1.1331484530668263, | |
1.0644944589178593, | |
1.0317434074991028, | |
1.0157477085866857, | |
1.007843097206448, | |
1.0039138893383475, | |
1.0019550335910028, | |
1.0009770394924165, | |
1.0004884004786945, | |
1.0002441704297478, | |
1.0001220777633837, | |
1.0000610370189331, | |
1.000030518043791 | |
] | |
k = 1.0 | |
z = x | |
ln_x = 0 | |
for i in range(len(lut)): | |
while z >= lut[i]: | |
z /= lut[i] | |
ln_x += k | |
k /= 2 | |
return ln_x | |
# Test the cordic_ln function | |
n = 2.0 | |
result = cordic_ln(n) | |
print("Natural logarithm of", n, "using CORDIC algorithm:", result) | |
print("Natural logarithm of", n, "using math.log:", math.log(n)) | |
# print(create_lut(16)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment