Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
CORDIC
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Software example (Python) === <syntaxhighlight lang="python3" line="1" start="1"> from math import atan2, sqrt, sin, cos, radians ITERS = 16 theta_table = [atan2(1, 2**i) for i in range(ITERS)] def compute_K(n): """ Compute K(n) for n = ITERS. This could also be stored as an explicit constant if ITERS above is fixed. """ k = 1.0 for i in range(n): k *= 1 / sqrt(1 + 2 ** (-2 * i)) return k def CORDIC(alpha, n): assert n <= ITERS K_n = compute_K(n) theta = 0.0 x = 1.0 y = 0.0 P2i = 1 # This will be 2**(-i) in the loop below for arc_tangent in theta_table[:n]: sigma = +1 if theta < alpha else -1 theta += sigma * arc_tangent x, y = x - sigma * y * P2i, sigma * P2i * x + y P2i /= 2 return x * K_n, y * K_n if __name__ == "__main__": # Print a table of computed sines and cosines, from -90° to +90°, in steps of 15°, # comparing against the available math routines. print(" x sin(x) diff. sine cos(x) diff. cosine ") for x in range(-90, 91, 15): cos_x, sin_x = CORDIC(radians(x), ITERS) print( f"{x:+05.1f}° {sin_x:+.8f} ({sin_x-sin(radians(x)):+.8f}) {cos_x:+.8f} ({cos_x-cos(radians(x)):+.8f})" ) </syntaxhighlight> ==== Output ==== <syntaxhighlight lang="console"> $ python cordic.py x sin(x) diff. sine cos(x) diff. cosine -90.0° -1.00000000 (+0.00000000) -0.00001759 (-0.00001759) -75.0° -0.96592181 (+0.00000402) +0.25883404 (+0.00001499) -60.0° -0.86601812 (+0.00000729) +0.50001262 (+0.00001262) -45.0° -0.70711776 (-0.00001098) +0.70709580 (-0.00001098) -30.0° -0.50001262 (-0.00001262) +0.86601812 (-0.00000729) -15.0° -0.25883404 (-0.00001499) +0.96592181 (-0.00000402) +00.0° +0.00001759 (+0.00001759) +1.00000000 (-0.00000000) +15.0° +0.25883404 (+0.00001499) +0.96592181 (-0.00000402) +30.0° +0.50001262 (+0.00001262) +0.86601812 (-0.00000729) +45.0° +0.70709580 (-0.00001098) +0.70711776 (+0.00001098) +60.0° +0.86601812 (-0.00000729) +0.50001262 (+0.00001262) +75.0° +0.96592181 (-0.00000402) +0.25883404 (+0.00001499) +90.0° +1.00000000 (-0.00000000) -0.00001759 (-0.00001759) </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)