Hacker News new | ask | show | jobs
by PaulHoule 1363 days ago
Yes and no.

The Taylor expansion works out like

   sin θ = θ - θ³/₆ + θ⁵/₁₂₀ - θ⁷/₅₀₄₀ + ⋯
if θ is in radians. This is ideal for small θ but if you want to cover, say, 0<θ<2π you are more likely to use something like

https://en.wikipedia.org/wiki/Chebyshev_polynomials

which are optimized across the range. You could rewrite these just as easily to work in degrees as radians.

One of the best ways to calculate sin and cos is CORDIC,

https://en.wikipedia.org/wiki/CORDIC

which is really based on turns, half-turns, quarter-turns and so forth.

2 comments

CORDIC is not based on radians or turns; it is based on decomposing the angle into a sum of:

phi_n = atan(2^-n)

and then using an abbreviated sum formula where computing cos(theta + phi_n) depends only on sums and bitshifts.

The small-angle approximations sin(x) ≈ x and cos(x) ≈ 1-x^2/2 are the real killer feature of radians, though, because when you can deal with the loss of accuracy you get to avoid using any loops whatsoever. They're also fundamental to understanding simple physical systems like a pendulum.

"One of the best ways to calculate sin and cos is CORDIC" This is extremely false. Cordic is 1 bit per iteration, while polynomials (Chebyshev or minmax) converge exponentially faster.
You are of course right about the speed, when a fast hardware multiplier is available for the computation of the polynomials.

On the other hand with CORDIC it is extremely easy to reach any desired precision, and in cheap hardware it does not require multipliers.

So CORDIC may be considered as "one of the best ways" depending on how "best" is defined.

Even when developing a polynomial approximation for the fast evaluation of a trigonometric function, it may be useful to use an alternative evaluation method by CORDIC, in order to check that the accuracy of the polynomial approximation is indeed that expected, because for CORDIC it is easier to be certain that it computes what it is intended.