Thread: Unexpected NAN error

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    6

    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?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    And the value of n would be?

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    n=995. Originaly an integer. Was not changed to 0.
    Last edited by jbjebi; 10-22-2011 at 11:34 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Are all your arrays of sufficient size?

    Were all values initialised before use?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    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.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    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..

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    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
    Last edited by jbjebi; 10-22-2011 at 01:41 PM.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    6
    oops censored again.. it was 'a' '**'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No error, but the result is unexpected
    By Nimbuz in forum C Programming
    Replies: 10
    Last Post: 07-24-2009, 03:10 PM
  2. Unexpected Error
    By Vendicate in forum Linux Programming
    Replies: 7
    Last Post: 04-04-2006, 12:01 PM
  3. unexpected end of file error?
    By Sway in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2002, 10:40 PM
  4. compiler Error: unexpected end of file....
    By RoD in forum Windows Programming
    Replies: 4
    Last Post: 09-13-2002, 03:46 AM
  5. Unexpected end of File Error
    By maxthecat in forum Windows Programming
    Replies: 5
    Last Post: 07-30-2002, 02:09 AM