Log in

View Full Version : atan Problem - Need Help


the_undefined
18-02-2005, 11:16
Hi Guys,

I have a little problem getting the atan function of Team 296's Math Library to work for me. What I need is calculating the Degree my Joystick is looking it by using atan(x,y).

I already know how to use the sin/cos function and how to work with the binary radians (-8,388,608 to 8,388,607) that the functions operate in.

But for some reason the results I get back from atan seem to be different from the one sin/cos give me or in different words: Say behave nonlinear in cases they should:

Here is what I tried (after I had problems with weird results):

short long brad; int int_brad;

// should give me 8388608/4 Brad = 45 Degree back, according to the way sin/cos work
brad = atan(1,1);

// Transform short long to int to be able to print the result with printf();
// Drop the last 8 Bit (Should be 32768/4=8192 after that)
brad>>=8;

// Put value in an Int
int_brad = (int)brad;

// Print the Value to the Screen
printf("int_brad = %d\n", (int)int_brad);

// ====Output===
int_brad = 8511


Converting this into Degree I got 46 Degree but I didn't like that because sin/cos worked way more accurate for me than this. So I tried different Values and that showed that there must be a problem:



atan(1,1) = 8511 (should be 32768/4=8192 = 45 Degree)
atan(1,-1) = 24256 (should be -32768/4=-8192 = -45 Degree)
atan(1,0) = 1799 (should be 0/32768=0) = 0 Degree)
atan(1,2) = 1799 (should be 11548) = 63.43 Degree)
atan(1,-2) = 1799 (should be-11548) = -63.43 Degree)
atan(2,1) = -1800 (should be 4736) = 26.57 Degree)
atan(2,-1) = -30969 (should be -4736 = -26.57 Degree)


Anybody out there that has an Idea what I could do wrong or somebody that already has atan working?

Thanks for your Help,

Felix