To save performance on sin
calls, and to handle integer angles, which are more portable manipulated and saved, instead of floating points as angles, I am building a sin
lookup function, where 4096 units equals 2pi radians. To save memory, I only store the first 1024 sin values, which are equivalent to sin( [0, pi/2) )
.
static const float SinTable[1024] = {0, 0.00153398, ..., 0.999995, 0.999999};
To handle angles in the 3rd and 4th quadrant, I simply conditionally negate:
return Angle&2048 ? -UnsignedSin : UnsignedSin;
Where UnsignedSin
is the looked up sin value wrapped between [0, 2048)
. But how can I handle the second and 4th quadrants? How can I properly map the stored sin values of [0, 1)
to [1, 0)
conditionally by checking if the angle is in the 2nd or 4th quadrants such as with Angle&1024
? I tried this but this is not quite right, because the result for 1024
angle is 0.999999 and not 1 which it should be.
const float UnsignedSin = SinTable[(Angle&1024 ? ~A : A)&1023];
The value of 1 is never stored in the sin table, so I assume a 1-SinTable[...]
is required? But I cannot get it quite right.