Thread: Help with "nan" for my output

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    28

    Help with "nan" for my output

    I'm writing a program that reads in values from an input file stores them into an array and computes the mean and standard deviation. The mean and standard deviation should be floating point with 2 decimal places. however when I compile using gcc -g -Wall, I get nan error in my output. Ive posted the code. could someone help me get the correct output. The program functions when I compile with gcc but not gcc -g - Wall.
    Last edited by bigparker; 06-15-2009 at 05:38 PM. Reason: more accurate program description

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Fix your program so it's taking numbers from some pre-determined array. If it works fine there, your file is messed up, or the way you're reading it is messed up. If it doesn't, then look at your actual functions that are manipulating the data.

    Long story short: Your program works with files we don't have. I'm not going to take the time munging together some fake data file just to test your program.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you getting any warnings? Try fixing them.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    87
    Quote Originally Posted by bigparker View Post
    I'm writing a program that reads in values from an input file stores them into an array and computes the mean and standard deviation. The mean and standard deviation should be floating point with 2 decimal places. however when I compile using gcc -g -Wall, I get nan error in my output. Ive posted the code. could someone help me get the correct output. The program function when I compile with gcc but not gcc -g - Wall
    At the top you assign 0 to mean2 and std, and never again give them a new value. That is why the program always reports a mean and standard deviation of 0. Further, "nan" stands for "not a number" and is a result of division by 0 in this case.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    28

    input file

    I don't get any warnings or errors. The program compiles fine. I have attached the input text file that I used for testing.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So did it work when you switched over to using an array? Or do you just want us to do everything for you?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I didn't compile the code, but just by looking at it, to begin with you're never actually calling the functions std_deviation or mean. Furthermore, the functions should return their actual values instead of storing them in global variables. Oh and you should be checking the return value of fscanf to detect file IO errors (ie: it should equal exactly 1 in this case).
    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;
    }

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    jason_m: i uninitialized those variables and it still gave me the 0.00 values. I know that the program functions and that its dividing by zero somewhere but i cant seem to find out where...


    its reading the file fine and storing the values into the array fine. The program worked when I was running it from the command prompt on the windows machine but when I switched to a linux machine which uses the gcc-g-Wall compiler thats when it starting giving me the nan values in my output.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should note that it's not possible for a program to work when using "gcc" and fail when using "gcc -g -Wall", so bear in mind that we all think you're either lying to us or deluded. (This does include unintentional lying, as it's possible that your Windows code was very different from your linux code.)

    That said, you should also note that your do-while loop to fill the array goes one too many places, and maybe if you uncommented out your calls to mean and std_deviation you'd be somewhere.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by tabstop View Post
    You should note that it's not possible for a program to work when using "gcc" and fail when using "gcc -g -Wall", so bear in mind that we all think you're either lying to us or deluded.
    Huh? I think the situation was some compiler on a windows machine and then some other compiler on another machine. That is enough to tell me that in fact, the program *could* work on one but fail on the other. However, the bad news is that it is still wrong (the program) and you are just getting lucky on the first machine.

    There are certain kinds of programming mistakes that *may not* cause a problem; that does not mean they are not mistakes, or that they will not or cannot cause a problem in the future on the same or a different machine.
    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

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    Huh? I think the situation was some compiler on a windows machine and then some other compiler on another machine. That is enough to tell me that in fact, the program *could* work on one but fail on the other. However, the bad news is that it is still wrong (the program) and you are just getting lucky on the first machine.

    There are certain kinds of programming mistakes that *may not* cause a problem; that does not mean they are not mistakes, or that they will not or cannot cause a problem in the future on the same or a different machine.
    I just quote the guy. It's possible he doesn't know what compiler he uses on Windows.

    But if there's a compiler out there that will compile and execute statements that are inside /* comments */, it's not a compiler he should be using (hence the statement that it is very possible that the Windows code looks nothing like what we're seeing).

  12. #12
    Registered User
    Join Date
    Jun 2009
    Posts
    28
    Thanks for all your help. I guess I came to the wrong place. Im new to C and pretty much a novice in programming period. I'm not looking for you guys to write my program for me. I'm simply stating what happened. There is no need for me to lie. I was just looking for some guidance to help me get my assignment completed. thank you...

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Agreed that there is no need for you to lie. So, don't, and everybody will be happy. If you have working code on Windows, it is not what you posted and you are compiling two very different files. If you don't have access to your windows files any more, then fine; in that case you should be looking for the guidance that has already been provided, about actually calling the functions you have written, reading from files, etc.

  14. #14
    Registered User
    Join Date
    May 2008
    Posts
    87
    Quote Originally Posted by bigparker View Post
    jason_m: i uninitialized those variables and it still gave me the 0.00 values. I know that the program functions and that its dividing by zero somewhere but i cant seem to find out where...


    its reading the file fine and storing the values into the array fine. The program worked when I was running it from the command prompt on the windows machine but when I switched to a linux machine which uses the gcc-g-Wall compiler thats when it starting giving me the nan values in my output.
    It could be that your compiler is initializing them to 0, but you're missing the point. What is the point of the variables if you never assign them a value? Especially if you use them in later calculations?

    The divide by zero happens on the line with division:
    Code:
    	printf("%3d %10d %10.2f\n",(b+1),data_in[b],(float)data_diff[b]/std);
    If it works on the windows machine, I have to believe it is simply because your compiled executable is based on a different version of your source code (maybe one where the lines assigning avg and sigma aren't commented out, but that's still not enough to make it work). I don't see how this could ever produce anything useful. So I think in that regard you are mistaken.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bigparker View Post
    Thanks for all your help. I guess I came to the wrong place. Im new to C and pretty much a novice in programming period. I'm not looking for you guys to write my program for me. I'm simply stating what happened. There is no need for me to lie. I was just looking for some guidance to help me get my assignment completed. thank you...
    Well, look around but I don't think it gets better than cboard or I wouldn't have spent so much time here.

    When you first start programming you really really really cannot* have any idea of some of the complications (and consequent "frustratrations") involved. I didn't even look at your code: #1) because you didn't post it, you attached it. #2) because yours follows a very familiar pattern whereby someone comes on, hands over their entire program (full of irrelevant detail) and some explanation of what the program is supposed to do and how "it isn't working right".

    You can continue with that method and some people will help you, but I promise you will get much quicker results if you can figure out how to write succinct, specific questions. To me, #1 and #2 are related -- you are too lazy to come up with a code snippet reproducing the problem, so you just attach your whole source. I'm not saying you are a lazy, stupid person by nature, just you are new to programming forums and need to "get with the program". Your skill level doesn't matter, it's your ability to communicate, and (more importantly, since this in itself is a programming skill) your ability to isolate and identify a problem.

    Other people here have recommended this before and so will I:
    How To Ask Questions The Smart Way

    Now look at this:
    Quote Originally Posted by you
    I'm writing a program that reads in values from an input file stores them into an array and computes the mean and standard deviation. The mean and standard deviation should be floating point with 2 decimal places. however when I compile using gcc -g -Wall, I get nan error in my output. Ive posted the code. could someone help me get the correct output. The program functions when I compile with gcc but not gcc -g - Wall.
    I don't care what you are doing unless it provides a context for your question, however, clearly the context you are providing is "please test compile my code and look at what happens". You need to put some more thought in.

    I guess your question *would* make sense if this could be explained away on the basis of some compiler vs. some other compiler, but we are here to tell you that is not the case. The C language is standardized. So forget about that side of things -- your problem is the nan output.
    But you do not bother to say where that output is produced, or provide a short snippet of the code that produced it.

    Good luck!

    @ tabstop: sorry, the OP does say "with gcc but not gcc -g - Wall" but seems to elaborate later into two different machines?


    * maybe you could, but if you did I think you would not even start
    Last edited by MK27; 06-15-2009 at 07:04 PM.
    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

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