Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Theoretically exact sine/cosine method #19

Open
nslay opened this issue Jul 4, 2018 · 3 comments
Open

Theoretically exact sine/cosine method #19

nslay opened this issue Jul 4, 2018 · 3 comments

Comments

@nslay
Copy link

nslay commented Jul 4, 2018

Very cool library! I found it mentioned in a constexpr stackexchange question. I like to dabble with low level math like this and I've derived a theoretically exact way to calculate sine/cosine by playing games with the half-angle formulae and bits. I've provided the derivation and a (probably poorly written) C++14 constexpr version (requires constexpr abs, sqrt, sgn, and modf). You might already know this trickery. I can't promise the C++14 version is correct (Clang 6 is happy with it) or if MSVC will tolerate it.

I'm not suggesting you use this. Your implementation using Taylor series (if I'm not mistaken) is probably a lot faster since this formulation needs to examine at most the number of mantissa bits (e.g. 52 for double precision)!

I think there is a name for this method. I don't remember off the top of my head. But good fun!

Thanks for the library!

Here's the demo code... I can just use your static math library from now on!
demo.txt

And below are notes I wrote myself if I ever look at this code again in the distant future.

NOTE: I didn't intend for the LaTeX pictures to automatically display.

Explanation
explanation

Pseudo Code
pseudocode

@ratchetfreak
Copy link

it's not going to be 100% exact though as rounding errors will accumulate and lose you up to log2(steps) = ceil(log2(54)) = 7 bits.

@nslay
Copy link
Author

nslay commented Jul 5, 2018

Indeed, it's only exact on paper. But it's exact on paper for finitely represented binary numbers using a finite number of operations. Contrast this with infinite series that need infinite terms to be exact on paper.

This kind of algorithm is seemingly related to CORDIC algorithms (that's the name I couldn't remember), although my derivation is different than CORDIC described on Wikipedia. That one appears to produce 1 bit of the output per iteration which I think is far more interesting than what I've derived.

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

Looks like CORDIC algorithms exist for most mathematical functions. Such algorithms might be good candidates for constexpr math.

@Morwenn
Copy link
Owner

Morwenn commented Jul 6, 2018

Monthly reminder that this library was a toy project and that I know next to nothing to floating point math internals and non-trivial math in general. The exponential, logarithmic, trigonometric and hyperbolic functions have been contributed by other people who are more knowledgeable in those areas than I am :p

Algorithms with fewer rounding errors do sound interesting but I am hardly in a position to judge what is actually better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants