Thread: Help with "nan" for my output

  1. #16
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> I was just looking for some guidance to help me get my assignment completed.

    Did you fix the problems that were already pointed out (including my earlier post)? Fixing those you'd be a whole lot closer to solving the problem, anyway.

    >> Thanks for all your help. I guess I came to the wrong place.

    It might be a good idea to develop thicker skin when dealing with message board advice. People around here handle a whole lot of (often inane) questions, and as a result have a tendency to be quite blunt. It's nothing personal.
    Last edited by Sebastiani; 06-15-2009 at 07:01 PM. Reason: bad was grammer
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  2. #17
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    i understand how things work here now. i fixed the things that you guys suggested and im getting values now. no more nan outputs. The only thing now is that even though I have specified that the values are to be floats. I am still getting whole numbers. here is my code:
    Code:
    /*mean accepts the current index position, the array
    pointer, and the count to determine the mean*/
    int mean(int index, int *data_in, int integer_count){
        float sum=0;
        float mean=0;
    
        for(index = 0; index!=integer_count; index++){
            sum = sum + data_in[index];
            }
            mean =(float) ((sum)/(integer_count));
            mean2 = (float)mean;
            return mean2;
    
        }
    
       }
    /* This function computes the standard deviation, sigma.*/
    int std_deviation(int i, int *data_in, int integer_count){
    int d,g = 0;
    
    
    float sq_diff = 0;
    float diff = 0;
    float variance;
    
    /*For loop to access the input array and uses integer count to piece together the std. formula*/
            for(d=0; d<integer_count;d++){
                g= data_in[d];
                diff = g - mean2;
                data_diff[d]= diff;
                sq_diff += diff*diff;
                data_diff[d] = (float) data_diff[d];
                }
          variance = sq_diff/integer_count;
          std = sqrt(variance);
    
         return std;
    }
    and this is the printf statement that i am using.
    Code:
    printf("Number of entries = %3d, mean = %3.2f, sigma = %3.2f\n",integer_count,(float)avg,(float)sigma);
    it seems to be something small that I am missing...

  3. #18
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bigparker View Post
    I am still getting whole numbers. here is my code:

    /* This function computes the standard deviation, sigma.*/
    int std_deviation(int i, int *data_in, int integer_count){

    Code:
    printf("Number of entries = %3d, mean = %3.2f, sigma = %3.2f\n",integer_count,(float)avg,(float)sigma);
    it seems to be something small that I am missing...
    sigma is an integer and you cast it into a float: is this an example of what you mean by "unexpected" whole number? An integer is a whole number...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #19
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    yes...I assumed if i casted the value as a float then it would give me the numeric values after the decimalpoint . But even with the cast if the mean is 3.5 it prints 3 and if sigma is 1.73 its prints 1...

  5. #20
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well of course. Once you put the result into an int the fractional part is lost. Casting it to float doesn't magically reproduce it. Just change the variables to float and you'll be fine.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #21
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    thank you guys sooo much!! I appreciate the tough love. It made me dig deeper than I thought I could...My program works fine now...

  7. #22
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    nan could be a division by 0, or sqrt of a negative number in the case of standard deviation.

    Things to do:

    read in your file, and then print it to another file and make sure they are the same. If they aren't, you are reading it in wrong.

    put printf statements in at each step of your calculation to see where things go wrong - if you find a negative number going into the sqrt, that's the problem.

    Make sure you initialized all your variables, garbage in memory could produce nans with some operations.

    EDIT: nvm, but things to keep in mind next time ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM

Tags for this Thread