Implementing Cosine in C from Scratch (2020)
21–30 of 139 posts
Re: Implementing Cosine in C from Scratch (2020)
#22lookup tables and lerp are the most common solution I've seen (for a wide range of functions). You can also memorize with an LRU cache.
> You can also memorize with an LRU cache. That's probably not going to perform well for an operation this fast. The computation is faster than main memory read.
Re: Implementing Cosine in C from Scratch (2020)
#23Mandatory Physics "troll" comment: For small angles, sin(x) ~= x and cos(x) ~= 1 -- the ["small angle approximation"]( https://en.wikipedia.org/wiki/Small-angle_approximation ) It's actually kind of ridiculous how many times it comes up and works well enough in undergraduate Mechanics.
Re: Implementing Cosine in C from Scratch (2020)
#24Doom?
Re: Implementing Cosine in C from Scratch (2020)
#25lookup tables and lerp are the most common solution I've seen (for a wide range of functions). You can also memorize with an LRU cache.
A common trick to make that faster is to not use doubles and radians to represent the phase. Instead, represent phase using an integer in some power-of-two range. That lets you truncate the phase to fit in a single period using a bit mask instead of the relatively slow modulo.
Re: Implementing Cosine in C from Scratch (2020)
#26>I couldn't find any notable projects on GitHub that use a table for trig functions, but I'm sure they exist. Doom?
Re: Implementing Cosine in C from Scratch (2020)
#27Re: Implementing Cosine in C from Scratch (2020)
#28https://en.wikipedia.org/wiki/Bhaskara_I%27s_sine_approximat...
Re: Implementing Cosine in C from Scratch (2020)
#29Here's a solution which is vectorizable: http://gallium.inria.fr/blog/fast-vectorizable-math-approx/ I've also read that Chebyshev polynomials are far better for approximating functions than Taylor series (eg https://en.wikipedia.org/wiki/Approximation_theory )