Thread: Problem with trig functions

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    6

    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));
    Thanks for your help

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    atan and atan2 return radians, not degrees.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring trig functions?
    By Raen in forum C Programming
    Replies: 6
    Last Post: 11-06-2010, 05:55 PM
  2. Trig Functions without math.h
    By daltore in forum C Programming
    Replies: 13
    Last Post: 12-29-2008, 04:47 PM
  3. Trig Help...
    By MasterGBC in forum C Programming
    Replies: 7
    Last Post: 03-22-2003, 11:39 AM
  4. inverse trig functions
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 02-22-2003, 11:20 AM
  5. trig
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 01-17-2002, 07:15 AM

Tags for this Thread