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

Hi,

If you look at the description of the cosine function in the VisualDSP++ Online Help for SHARC, you will find that for 32-bit floating point numbers the input domain is [-1.647e6, 1.647e6 == 2^19 * pi ]. For 64-bit floating point numbers, this domain increases to [-8.433e8, 8.433e8 == 2^28 * pi].

These limits are imposed by the approximation algorithm used to implement the cosine function. Dealing with a periodic signal, the first step in the algorithm is to perform an argument reduction - that is if your input value is (n*2*pi)*alpha to find alpha' such that 0<alpha'<pi/2 and abs(cos((n*2*pi)*alpha)) == abs(cos(alpha')) hold true (basically finding the equivalent point in the first quadrant of the cosine graph). At some point a floating point number will grow so large that the gap between two subsequent numbers will be such that it is no longer possible to perform the argument reduction. If this happens, the cosine function will abort, returning 0.

Andreas