Advertisement

Fixed-Point Math

Started by September 07, 2002 02:44 AM
7 comments, last by Stunad 22 years, 5 months ago
I am programming on mobile devices who's processor's can't handle floating point numbers so for speed I am converting over to fixed point math. One calculation that I am having trouble performing without using floats is finding the angle a 2D vector makes with the x-axis, arctan(-y/x). Is their anyway I can perform this calculation without using floats? Thanks. Chris De Salvo Two Stunads & Co. www.twostunads.com [edited by - Stunad on September 7, 2002 3:48:20 AM]
Chris De SalvoTwo Stunads & Co.www.twostunads.com
You could just use lookup tables if there is enough memory to make it possible.
Advertisement
I used look-up tables with cos and sin values which worked perfectly, but for those trig functions I used the angle value to lookup their values. But because of the nature of arctan I was having trouble creating a look-up table. Any suggestions on how to do this?
Chris De SalvoTwo Stunads & Co.www.twostunads.com
Show us the formulas you are calculation, and it might be easier then.
a response curve might be the sweet spot between accuracy and performance, especially if you allow changing bucket sizes

********


A Problem Worthy of Attack
Proves It's Worth by Fighting Back

[edited by - walkingcarcass on September 7, 2002 2:02:54 PM]
spraff.net: don't laugh, I'm still just starting...
This is the code I am using to find the angle a vector makes with the x-axis:


  int Vangle() {   angle=atan(-y/x)*(180/3.14159);   if(x<0)   {      angle+=180;   }   else if(y>0)   {      angle+=360;   }		   return angle;}    


x and y are the components of the velocity vector and are floats. This works, I would just like to avoid using the floats.
In my other functions that calulate for instance the magnitude of a vector I converted the x and y components to ints using fixed point math and used cos and sin lookup tables and converted those lookup values to ints, so I am able to do all my other vector math at this stage only using integer values.
I just can't figure out how to use atan() in my calculation while avoiding floats.

Thanks for the replies! This is driving me crazy!

BTW, the function is in a class. Thats why the function takes no parameters and none of the variable are declared.

[edited by - Stunad on September 7, 2002 3:13:30 PM]
Chris De SalvoTwo Stunads & Co.www.twostunads.com
Advertisement
Why couldn''t you make a lookup table for atan()?
maybe you should considering forming the atan(x/y) into atan2(x, y)
Doing a lookup for arctan is similar for cos, sin etc. The only problem is that in

x = arctan(y)

y can range from - to + infinity, to large for any simple lookup table.

to solve this: for x between 0 and 45 tan(x) ranges from 0 to 1, and for x between 45 and 90 it goes from 1 to infinity.

The connection between these two ranges is

tan (x) = 1/tan (90 - x).

This gives a formula of

arctan (y) = 90 - arctan (1/y)

Using this formula you only need a table for values betweeen 0 and 1. Using this the function looks like

FIXED arctan (FIXED y)
{
// deal with negative y
if (y < 0) return -arctan (-y);
// deal with y > 1
if (y > ONE) return 90 - arctan(ONE / y);
// y between 0 and 1: use lookup table
return atan[y * LOOKUP_TABLE_SIZE];
}

using fixed arithmetic where appropriate. Generate the table using a simple floating point program. Replace the 90 degrees with pi/4 if you are working in radians.
John BlackburneProgrammer, The Pitbull Syndicate

This topic is closed to new replies.

Advertisement