This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

atan gets a wrong result

I'm tring to do some calculation with atan() but it kept getting wrong results, same thing happened with sin() and cos(). I'm working under CCS3.3 with TMS320c6701. I thought  the optimization mayber the problem so i turn it off, but I still got wrong result.

Could anyone help me? Thank you! 

  • CCS v3.3 is very old.  If at all possible, please update to CCS v6.  

    Note the compiler version is different from the CCS version.  What compiler version are you using?  Please post an example that compiles.  It does not need to link or run.  Please show all the compiler options you use.

    Thanks and regards,

    -George

  • Did you remember to include math.h?

    How wrong is it?  Is it close, but wrong in the last few bits, or is it wildly wrong?

  • George

    The compiler version is v6.0.8.

    built options: -g -k -ss -o3 -mt -mw -mv6700

    My code tried to calculate atan(-0.774) which should be -0.6587,but it got -0.1216. 

    The code works fine with Simulator.

    I wrote a test function to calculate sin()

    #include <math.h>

    #define PI 3.1415926

    void test_sin()

    {

     unsigned int i;

    volatile double val_sin[21]

    for(i=0; i<21; i++)

    val_sin[i]=sin(PI*i/16.0);

    }

    This is the result I see from the watch window(The results are longer, I didnt type all the digits)

    val_sin[0]=0.0

    val_sin[1]=0.19509032

    val_sin[2]=0.38268341

    val_sin[3]=0.55557023

    val_sin[4]=0.70710678

    val_sin[5]=0.83146961

    val_sin[6]=0.92387953

    val_sin[7]=1.0

    val_sin[8]=1.0

    val_sin[9]=1.0

    val_sin[10]=1.0

    val_sin[11]=0.9817477

    val_sin[12]=0.78539816

    val_sin[13]=0.58904862

    val_sin[14]=0.39269908

    val_sin[15]=0.19634954

    val_sin[16]=3.58979302e-009

    val_sin[17]=-0.19634954

    val_sin[18]=-0.39269908

    val_sin[19]=-0.58904862

    val_sin[20]=-0.78539816

    The result is wrong from val_sin[7] (except for val_sin[8] and val_sin[16]  maybe). Results changed when I ran it again and again. Sometimes I got  NaN.

    Also, similar thing happened to atanf() and sinf(), like atanf(-0.774) return -0.774

  • Archaeologist 

    Yes, I included math.h

    It's not wrong all the time, it gave a right result to atan(1), but when my code tried to get atan(-0.774) , it gave a wrong result. Sometime it gave a very large number like 2.766425e315.

  • Xiawei Chen said:
    The compiler version is v6.0.8.

    That compiler is 8 years old.  Upgrade if at all possible.  Please see this page.

    Xiawei Chen said:
    Results changed when I ran it again and again. Sometimes I got  NaN.

    This is probably not a compiler problem.  Be sure you are not overflowing the stack.  There might be some other HW problem.

    Thanks and regards,

    -George

  • I'm sorry, I cannot reproduce the problem.  Since it runs fine on the simulator, it is unlikely to be a compiler problem.  You should check that the values displayed in the watch window accurately reflect the computed values.  Try printing the values and comparing the results to what you see in the watch window:

    #include <stdio.h>
    #include <math.h>
    
    #define PI 3.1415926
    
    void main()
    {
        unsigned int i;
    
        volatile double val_sin[21];
    
        for(i=0; i<21; i++)
            val_sin[i]=sin(PI*i/16.0);
    
        for(i=0; i<21; i++)
            printf("sin(%d*PI/16) = %f\n", i, val_sin[i]);
    }
    
  • George

    Do rts functions have to be in internal memory? I put them in external memory.

    I increased the stack's size from 0x400 to 0x1000, the code still didn't work.

    Then I moved the rts functions to internal memory, the code worked!

    Then I decrease the stack's size back to 0x400, the code didn't work. You are right about the stack overflowing

    Any explanations?

    Thank you

    Chen

  • For C6000, functions may be in either internal or external memory.

    It's unlikely that the program was executing correctly just because the functions were in internal memory; more likely, the other sections of the program were placed somewhat differently, and the program was somehow able to avoid the problem.

    I can't really determine what the real problem is; it may still be lurking in your program.  We would probably need a complete program which reproduced the problem to be able to analyze it.