Skip to content

Instantly share code, notes, and snippets.

@syadlowsky
Last active April 6, 2016 05:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syadlowsky/6796a7dd0206cb3876362158253c574e to your computer and use it in GitHub Desktop.
Save syadlowsky/6796a7dd0206cb3876362158253c574e to your computer and use it in GitHub Desktop.
c_0 = -0.969474842881623111168210
c_2 = 4.364528972627628178315717
c_4 = -2.422793242101592081638728
function fast_cos(t::Real)
# Computes cos(pi*t) using a Chebyshev approximation.
# This approximation is minimax optimal on [-1, 1].
# By scaling by pi, we find an optimal approximation on
# the entire range of cos(x). Because cos(x - pi) = -cos(x),
# we can take the remainder with 2, and shift [0, 2] |-> [-1, 1],
# taking the negative of the coefficients to compute -cos(x)
# instead. This is a 4th order approximation.
cos_t::Real;
t = rem(t, 2.0) - 1.0;
cos_t = c_0 + c_2 * t^2 + c_4 * t^4;
return cos_t;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment