-
Unexpected NAN error
hello. I need help on NAN error.
I have two data set of 2*2 array and want to calculate average and standard error from them.
the arrays are Ldata[i][j] and Rdata[i][j], of which each element is a structure.
'i' runs from 0 to 95 and 'j' runs from 0 to 994.
I calculated average and average of square for each i, that is, sum over j(0~994) and get 96*2 numbers.
My code is like this :
Code:
for(j=0;j<T_SUP;j++){
for(i=0;i<n;i++){
Lavg[j]+=Ldata[i][j].Re;
Ravg[j]+=Rdata[i][j].Re;
Lavgsqr[j]+=(Ldata[i][j].Re)*(Ldata[i][j].Re);
Ravgsqr[j]+=(Rdata[i][j].Re)*(Rdata[i][j].Re);
}
Lavg[j]/=(double)n;
Ravg[j]/=(double)n;
Lavgsqr[j]/=(double)n;
Ravgsqr[j]/=(double)n;
}
here T_SUP=96, n=995.
It is a simple code, but the results i got had errors.
part of the results is like this :
Code:
1.65927877636165E+02 2.76070821725818E+04
1.26235062991787E+02 1.59824584847397E+04
9.61712581846613E+01 9.27823852576923E+03
7.33170496897583E+01 5.39356871353874E+03
5.59046671535362E+01 3.13669103534178E+03
4.26358025373460E+01 1.82497347823173E+03
3.25324479306918E+01 1.06288515111161E+03
2.48371361006079E+01 6.19748708397692E+02
NAN 3.61295254889204E+02
1.44695804797956E+01 2.10526428400531E+02
1.10431099231493E+01 1.22679873320664E+02
8.42562467527492E+00 7.14489220048574E+01
See NAN here? It happens only at j=18 and j=90 from 'Lavg' variable.
Lavgsqr, Ravg has no such error..
Of course I tried checking Lavg[i][18] but it was totally OK, and now I'm confused. I think that this code is symmetric for variables and indices so I wonder how can error like this occur. Can it be from floating point number operation?
I desperatly need help. Any suggestion for how this happened and how to solve it?
-
And the value of n would be?
-
n=995. Originaly an integer. Was not changed to 0.
-
Are all your arrays of sufficient size?
Were all values initialised before use?
-
I assigned memory by malloc() and checked if it doesn't return NULLthen so memory is OK, and if if array size was insufficient, the compiler would have printed warnng or error..
When i printed Ldata[i][18] it worked well so i beleave i'm not using any trash value, and Lavgsqr has no such problem.
-
You'd better post some more code then.
Because there are plenty of ways to screw this up which don't generate compile-time warnings, or run-time warnings.
-
common.h :
Code:
#ifndef COMMON_H
#define COMMON_H
#define LDATFILE "eight.bi.op.left.m0.03000.m0.03000.PxP"
#define RDATFILE "eight.bi.op.right.m0.03000.m0.03000.PxP"
#define T_SUP 96 //supremum of t (L value)
#define STERR(avg,avgsqr,n) sqrt(((avgsqr)-((avg)*(avg)))/((double)n-1.0))
#endif
typedef struct data
{
int t;
double Re;
double Im;
} Data;
extern void errmsg(int);
main.c :
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "common.h"
int main(){
int i,j,n;
char str[50];
double* Lavg,* Lavgsqr,* Lsterr;
double* Ravg,* Ravgsqr,* Rsterr;
FILE* Lfp,* Rfp,* ofp;
//Count the number of the data
Lfp=fopen(LDATFILE,"r");
Rfp=fopen(RDATFILE,"r");
if(Lfp==NULL||Rfp==NULL) {errmsg(1); return 1;} //if no such file in directory
n=0;
while(!feof(Lfp)){
fscanf(Lfp,"%s",str);
if(!strcmp(str,"TYPE:")) n++;
}
fclose(Lfp);
fclose(Rfp);
//Assign memory to the data
Dat.......... Ldata,** Rdata;
Ldata=(Dat..........)malloc(n*sizeof(Data*));
Rdata=(Dat..........)malloc(n*sizeof(Data*));
if(Ldata==NULL||Rdata==NULL){errmsg(2); return 2;} //if memory allocation failed
for(i=0;i<n;i++){
Ldata[i]=(Data*)malloc(T_SUP*sizeof(Data));
Rdata[i]=(Data*)malloc(T_SUP*sizeof(Data));
if(Ldata[i]==NULL||Rdata[i]==NULL){errmsg(2); return 2;}
}
//Write the data to the assigned memory
Lfp=fopen(LDATFILE,"r");
Rfp=fopen(RDATFILE,"r");
for(i=0;i<n;i++){
fgets(str,50,Lfp);
fgets(str,50,Rfp);
for(j=0;j<T_SUP;j++){
fscanf(Lfp,"%d",&Ldata[i][j].t);
fscanf(Lfp,"%lf",&Ldata[i][j].Re);
fscanf(Lfp,"%lf",&Ldata[i][j].Im);
fscanf(Rfp,"%d",&Rdata[i][j].t);
fscanf(Rfp,"%lf",&Rdata[i][j].Re);
fscanf(Rfp,"%lf",&Rdata[i][j].Im);
}
fseek(Lfp,1,SEEK_CUR);
fseek(Rfp,1,SEEK_CUR);
}
fclose(Lfp);
fclose(Rfp);
//Assign memory to statistical variables
Lavg=(double*)malloc(T_SUP*sizeof(double));
Lavgsqr=(double*)malloc(T_SUP*sizeof(double));
Lsterr=(double*)malloc(T_SUP*sizeof(double));
if(Lavg==NULL||Lavgsqr==NULL||Lsterr==NULL) {errmsg(2); return 2;}
Ravg=(double*)malloc(T_SUP*sizeof(double));
Ravgsqr=(double*)malloc(T_SUP*sizeof(double));
Rsterr=(double*)malloc(T_SUP*sizeof(double));
if(Ravg==NULL||Ravgsqr==NULL||Rsterr==NULL) {errmsg(2); return 2;}
//Statistical analysis
for(j=0;j<T_SUP;j++){
for(i=0;i<n;i++){
Lavg[j]+=Ldata[i][j].Re;
Ravg[j]+=Rdata[i][j].Re;
Lavgsqr[j]+=(Ldata[i][j].Re)*(Ldata[i][j].Re);
Ravgsqr[j]+=(Rdata[i][j].Re)*(Rdata[i][j].Re);
}
Lavg[j]/=(double)n;
Ravg[j]/=(double)n;
Lavgsqr[j]/=(double)n;
Ravgsqr[j]/=(double)n;
Lsterr[j]=STERR(Lavg[j],Lavgsqr[j],n);
Rsterr[j]=STERR(Ravg[j],Ravgsqr[j],n);
}
ofp=fopen("temp.dat","w");
for(j=0;j<T_SUP;j++){
fprintf(ofp,"%.14E %.14E\n",Lavg[j],Lavgsqr[j]);
}
return 0;
}
this is my main code..
-
What is this?:
Code:
Dat.......... Ldata,** Rdata;
Ldata=(Dat..........)malloc(n*sizeof(Data*));
Rdata=(Dat..........)malloc(n*sizeof(Data*));
Did you try and censor that or something?
You're using eof to control a loop. There's an FAQ entry about what is wrong with that.
You should also read the FAQ entry about casting malloc, or rather why you should not.
-
wow, I can't beleave that I really didn't initialize the variables..maybe I had been an amoeba or something.
Thanks Salem and everyone!
amd iMalc, thos dots were originally '..........' so maybe the board censored it lol
-
oops censored again.. it was 'a' '**'.