AnsweredAssumed Answered

Understanding error when generating cos (value)

Question asked by MikeSmithCanada on Mar 1, 2011
Latest reply on Mar 3, 2011 by AndreasL

What are the specifics of why cos (value) fails (returns 0) when double value is greater than about 2 million

 

Whats so specific about 2 million as a floating point number

 

I was also curious to see that cos(value) does not fail (at least for the time I was running the code) when using long double value

 

I was outputing a cosine signal to right channel 2 on a SHARC 21469 eval board (following the signal with a scope) using the following code when I found the error

 

const float deltaT = 1.0 / 10000;
unsigned int count = 0;   
double value = 0;
float signal = 0;

 

float HeartSignal(void) {
   
    count++;
    value = 2 * PI * 3600 * count * deltaT;
   
    signal = 0.1 * cos(value);

    if (signal > 0.1) {

       printf("stop\n");

   }

  //if (count >= 10000) count = 0;

    return signal;
}

 

ANother weird thing -- I can get the hardware to generate a sin signal amplitude value greater than 0.1 under some circumstances with the same code (uncomment if to ensure the overflow error does not occur). The software is not doing it as I can put a breakpoint at the printf (stop) and the code is never reached.  I think it is the way the hardware is reacting to a discontinuity in the smoothness of the sine wave

Outcomes