Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Problem with complex.h?

Hi,

Running this program on Linux, I've got what appears to be a memory allocation error with complex.h. Here's my snippet of code (which I'm using as part of an R program):



#include
#include
#include
#include

double CalcPer( double *input, long *N, int *L, double *deltat, double *output ){
int i, t;
// index variables
double *f, *SHat;
double complex sum, suma, blah;

f = ( double * ) calloc( *L+3, sizeof( double ) );
SHat = ( double * ) calloc( *L+3, sizeof( double ) );
for( i = 1; i <= *L; i++ ){
// we're not doing f=0
f[ i ] = ( ( double ) i )/( 2 * *deltat * *L );
sum = 0.0 + 0.0I;
suma = 0;
for( t = 1; t <= *N; t++ ){
sum = sum + input[ t-1 ] * cexp( -I * 2 * M_PI * f[ i ] * ( (double) t ) * *deltat ); \ line (a)
blah = input[ t-1 ] * cexp( -I * 2 * M_PI * f[ i ] *
t * *deltat );
suma = suma + blah;
sum2 = sum2 + blah2;
if( i == 1 && t == 1 ) printf( "%lf, %lf, %lf, %lf, %lf
",
input[ t-1 ], creal( sum ), cimag( sum ), creal(
suma ), cimag( suma ) );
}

So, sum and suma should be the same, right? The thing is, after running through this program three times (it's part of another program), suddenly sum = NaN. What's really odd is that if I comment out line (a) above (i.e., "sum = sum + ...", I then get suma = NaN. This seems to obviously be a memory allocation error due to the double complex data type. Any ideas of what I should do?



Thanks so much,

Nandor


Sign In or Register to comment.