3 ms·
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
by schappim 4y ago
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 https://github.com/ifduyue/musl/blob/master/src/math/cos.c
- FabHK 4y agoIt’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.
- deleted 4y ago[deleted]
- kolbe 4y agoWhy 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 https://github.com/matthewkolbe/LitMath
- jcranmer 4y agoUsing the expression ensures that the FP exceptions are properly set. It also does quieting of sNaN or NaN payload propagation as appropriate.
- hurrrrrrrr 4y agoIt'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 https://en.cppreference.com/w/cpp/numeric/fenv/FE_exceptions