# Thread: Problem with trig functions

1. ## Problem with trig functions

It's a pretty simple conversion from three numbers to two angles. tempx,tempy,tempx and magnitude all show up properly but phi and theta both give outputs of zero. I've tried changing them to floats and type casting them as ints but I can't seem to figure it out. Relevant snippets of my code are posted below

Code:
```float tempx, tempy, tempz, magnitude;
int phi, theta;

tempx=x/sqrt(x*x+y*y+z*z);
tempy=y/sqrt(x*x+y*y+z*z);
tempz=z/sqrt(x*x+y*y+z*z);
magnitude=sqrt(tempx*tempx + tempy*tempy + tempz*tempz);

phi=atan2(tempy,tempz);
theta=atan(-tempx/sqrt((tempy*tempy)+(tempx*tempx)));

if(phi>359)
phi=359;
if(phi<0)
phi=0;
if(theta>120)
theta=120;
if(theta<0)
theta=0;
UARTprintf("phi=%3d\t",phi);
UARTprintf("theta=%3d\n",(int)(theta*1000));
//panning
fPulseWidth3=(phi+360.0)/360;
//tilting
fPulseWidth2=(206.4-theta)/96;

UARTprintf("fPulseWidth3=%d\tfPulseWidth2=%d\n",fPulseWidth3,(int)(fPulseWidth2*1000));```

2. Have you tried printing phi and theta as floats or doubles? You need to use %lf with printf style functions to print a float/double. If you cast or let it convert automatically to an int, it takes the whole number part and throws away any fractional part. Also realize that atan and atan2 work in radians, not degrees, so you wont ever see anything greater that +/- pi. That means you're quite likely to get a phi or theta with an absolute value less than 1, so it comes up zero when converted to an int.

3. With printf, you use %f to print a float or double (floats are converted to doubles for printing).

Make phi and theta floats, since atan returns a double. (Actually, it's generally better to use doubles if you can get away with it.)

Also, note the lines
Code:
```if (phi<0)     phi=0;
if (theta<0)   theta=0;```
So if phi or theta are negative they'll end up as zero.

4. atan and atan2 return radians, not degrees.