|
Trig functions and type conversion
I'm trying to use the CORDIC function to do some basic trig (calculating an angle given an x and y coordinate from a joystick), and I'm getting some odd results.
In short, if I calculate the angle using the atan() function, the results work when y>0, but give incorrect and discontinous results if y<0.
Here's the simplest code that I came up with the recreates the problem:
#include "cordic-math.h" // renamed so that I know it's not the Microchip one
short long joyx,joyy;
short long theta;
joyx=(255-p1_x)-127;
joyy=p1_y-127;
theta=atan(joyy,joyx);
printf("theta: %d ",(int)(theta>>16));
Anyone have some thoughts? I at first thought it was a problem with negative numbers and the >> operator, but theta/65536 gives the same
numbers.
|