Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unexpected NAN error (C language)

    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?

    P.S. Solved. I simply didn't initialize the variables. Now I feel retarded..
    Last edited by jbjebi; 10-22-2011 at 08:40 PM. Reason: solved

  • #2
    Regular Coder bobleny's Avatar
    Join Date
    May 2007
    Posts
    256
    Thanks
    3
    Thanked 11 Times in 11 Posts
    If you run your code with the last four lines commented out like below:
    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;
    }
    That will narrow your results down. If you no longer get the error, then you know it is occurring in in the last four lines. If not, then you only need to deal with the other four lines of math. I'm guessing your error is with the first four lines. In which case try running this:
    Code:
    for(j=0;j<T_SUP;j++)
    {
    	for(i=0;i<n;i++)
    	{
    		printf("%e += %e\n", &Lavg[j], &Ldata[i][j].Re);
    		printf("%e += %e\n\n", &Ravg[j], &Rdata[i][j].Re);
    
    		printf("%e += (%e * %e)\n", &Lavgsqr[j], &Ldata[i][j].Re, &Ldata[i][j].Re);
    		printf("%e += (%e * %e)\n\n\n", &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;
    }
    That will let you see all of your numbers. If you get back to many to sort through, you can limit your loops to within the range of the error. This will let you know exactly what your numbers are. It may also be helpful if you post the relative output of this print statement.
    --www.firemelt.net--
    * No good deed goes unpunished.
    * Cheer up, the worst has yet to come...
    Get Firefox!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •