Hello all,
I have a fixed-point function (16.16) that calculates SIN and COS from a lookup table. However, since the LUT is in the range 0-360 (degrees), how do I convert an arbitrary angle (e.g. 535.2) to the range 0-360.
Usually a simple MOD would do it:
Code:
angle2 = angle % 360;
However in fixed point, you can't just do that, so a way to do it is:
Code:
angle2 = angle - fixmul(fixfloor(fixdiv( angle,360 )),360);
// same as (floating point):
angle2 = angle - ( floor( angle/360 ) * 360 );
But surely there is a faster way?
EDIT: Here is the whole function which looks up sin in a LUT which has one value for each degree, then does linear interpolation for partial degrees (e.g. 120.75 degrees).
Code:
fixed fixsin(fixed x){
int a, b;
fixed c, d, val;
// First ensure value is 0-360 (not including 360)
x = x - itofix(fixfloor(fixdiv( x,FIX360 )) * 360);
if (x<0){ x=-x; }
// Get the index directly before and after point
a = fixfloor(x);
b = a+1;
if (b>359){ b=0; }
// Get the sin values directly before and after point
c = _sin_lut[ a ];
d = _sin_lut[ b ];
// Do linear interpolation to get actual value
return c + fixmul((d-c),(x-itofix( a )));
}