News for hackers. Stuff that matters.
Re:Sin and Cos: The Programmer's Pals by vanderZwan on Tuesday March 06, 2018 @10:44AM (#16529485)
Do you have advice for which fast, good-enough approximations one can use instead in the situations where the precision is not needed?

In many use-cases we would pre-multiply the value we throw into sin and cos with two times Pi anyway, so couldn't one skip the circle constant altogether and a write function like sin_one(t) and cos_one(t), where each call "implicitly multiplies" t by two times Pi? (perhaps, if constructed well, the function could even be more precise, since it avoids the rounding errors, however small, of multiplying by the pi constant)

Re:Sin and Cos: The Programmer's Pals by stochastic_monk on Tuesday March 06, 2018 @11:26AM (#16529817)
I don't have advice on the specific `sin(x) / x`/`cos(x) / x^2`, but the Sleef vectorization library [0,1] supports operations with varying precision. (0.506, 1.0, and 3.5 ULP)

If you're working in a case where you can afford slightly lower accuracy, the 3.5ULP method can be a very significant speedup, especially since you're vectorizing at the same time. I got dozens of times in speedup when testing just the 1.0 ULP bound when performing sin/cos on a large vector. (I don't remember the precise number and should just re-run the tests.)

To generically dispatch this library, in case there's any interest, I wrapped it in some template metaprogramming in [2], so that the widest vectorization available is selected at compile-time when selecting a given operation.

[0]: http://sleef.org/

[1]: https://github.com/shibatch/sleef

[2]: https://github.com/dnbaker/vec

Re:Sin and Cos: The Programmer's Pals by vanderZwan on Tuesday March 06, 2018 @12:40PM (#16530392)
Thanks, forwarded your comment to a few colleagues who might have a use for it!
Re:Sin and Cos: The Programmer's Pals by 0xfaded on Tuesday March 06, 2018 @06:58PM (#16533250)
In most cases a Chebyshev approximation will just work. There is advice out there which says to use Clenshaw's summation formula to sum the Chebyshev polynomials, however I've found that expanding out the polynomial coefficients to get something like a5 x^5 + a4 x^4 + a3 x^3 to be more accurate in practice.

Scaling the polynomial, as you suggest, would simply require reevaluating the coefficients. For example, a5 (2pix)^5 would become 32 a5 pi^5 x^5.

In the cos case where regions of interest approach zero, divide by another polynomial with the same zeros to create limits. cos(x)/(pi^2/4 - x^2) -> 1/pi as x -> pi/2. Then approximate that function, and evaluate approx(x) \* (pi^2/4 - x^2), so you are multiplying the approximated value by something close to zero. This way you get precision in a way similar to sin(x) = x when x small.