News for hackers. Stuff that matters.
Re:Cosine Implementation in C by schappim on Friday March 31, 2023 @12:06AM (#35382418)
This is a faster implementation of cosine that utilises six static const double variables (C1, C2, C3, C4, C5, C6) to speed things up. These variables represent the coefficients of a polynomial approximation for the cosine function.

For the genuine article, check out: https://github.com/ifduyue/musl/blob/master/src/math/cos.c

Re:Cosine Implementation in C by FabHK on Friday March 31, 2023 @05:06AM (#35384426)
It’s the core implementation for a specific and very narrow range of arguments. The “genuine article” moves the argument into that range (via the rem_pio2 function performing range reduction, which is worth an article in itself), and then calls this core implementation.
Re:Cosine Implementation in C by kolbe on Friday March 31, 2023 @10:15AM (#35387478)
Why do you return x-x for inf or nan? I can't test now, but I imagine they both return nan?

I run a high performance math library in C#, and I'm always looking to improve.

https://github.com/matthewkolbe/LitMath

Re:Cosine Implementation in C by jcranmer on Friday March 31, 2023 @10:55AM (#35388049)
Using the expression ensures that the FP exceptions are properly set. It also does quieting of sNaN or NaN payload propagation as appropriate.
Re:Cosine Implementation in C by hurrrrrrrr on Friday March 31, 2023 @10:55AM (#35388051)
It's an easy way to return nan and set FE_INVALID. If you're not worried about floating point exceptions you can just return NaN directly. Idk how much C# even guarantees about the floating point operations and if it's applicable there.

https://en.cppreference.com/w/cpp/numeric/fenv/FE_exceptions