user2485710
user2485710

Reputation: 9801

How to compute a sine with binary angles?

How trigonometry works with binary angles ?

Assuming that my 360° or 2pi angle goes from 0 to the maximum value for a given unsigned integer type, how to compute the sin and all the other functions with this representation ?

What is the generic algorithm ?


EDIT

my point is to drop any floating point computation or floating point type, I would like to implement this with unsigned integers only.

Upvotes: 1

Views: 2068

Answers (2)

yota
yota

Reputation: 2220

I once implemented this, based on a CORDIC algorithm. For Binary angles, the algorithm is just trivial.

Let a n-bits binary angle and a vector [cos(x), sin(x)] initialized for x = 0. For each bit, from MSB to LSB, just apply a rotation of a fraction of tau (or pi). Rotation matrices can be precomputed. The algorithm spend at most 4 additions and 2 multiplications, times the numbers of bits set in your binary angle.

However ! the real problem is, how do you represent the resulting values of your sin(x) and cos(x). They can be fixed point, or floats... the main problem being to implement efficiently additions and multiplications for them.

Upvotes: 1

Pascal Cuoq
Pascal Cuoq

Reputation: 80276

I am going to assume that is your unsigned type goes up to UINT_MAX, you mean that 360° stands for the power of two UINTMAX + 1.

In this case it is very simple: use the standard-ish trigonometric function sinpi() which, when applied to x, computes sin(πx).

One example with 32-bit unsigned int:

sinpi((double)n * 0x1.0p-31)

Note that the conversion to double and the multiplication are both exact, meaning that the end result is as accurate as sinpi() is. You can find a maximally accurate double-precision sinpi within CRlibm.

Upvotes: 1

Related Questions