Thread: Fixed point MOD (for sin LUT)

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    36

    Fixed point MOD (for sin LUT)

    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 )));
    }
    Last edited by Pea; 04-10-2005 at 07:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixed Point Conversion
    By arkay in forum C Programming
    Replies: 1
    Last Post: 02-04-2005, 10:26 AM
  2. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM
  3. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM
  4. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 11:34 PM