nan disappear when printing

This is a discussion on nan disappear when printing within the C Programming forums, part of the General Programming Boards category; Hello, I was hoping you would be able to help me with a problem which I don't understand. At some ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    11

    Question nan disappear when printing

    Hello,
    I was hoping you would be able to help me with a problem which I don't understand.
    At some point in my program I am getting a not-a-number value for a certain parameter which later causes an error.
    However, when I was trying to find the reason for the nan by printing the parameters that compose it - the problem disappears. All the parameters seem to have a value and the error is gone.
    Any advice as for how to start debugging this???
    Many thanks!
    Liat.

  2. #2
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    How about posting the code? My crystal ball is on vacation.
    When the eagles are silent, the parrots begin to jabber. ~Winston Churchill

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Let me ask you a question: If your car breaks down, do walk into the mechanic's shop without it, and go: "Hey, my car's broken. Can you fix it for me?"

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    >Let me ask you a question: If your car breaks down, do walk into the mechanic's shop without it, and go: "Hey, my car's broken. Can you fix it for me?"
    I would. How would you get to the shop?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,980
    I would. How would you get to the shop?
    In the tow truck?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I'm poor.



    okay you're right.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by liats80 View Post
    Hello,
    I was hoping you would be able to help me with a problem which I don't understand.
    At some point in my program I am getting a not-a-number value for a certain parameter which later causes an error.
    However, when I was trying to find the reason for the nan by printing the parameters that compose it - the problem disappears. All the parameters seem to have a value and the error is gone.
    Any advice as for how to start debugging this???
    Many thanks!
    Liat.
    If adding print statements for debugging changes the program behavior, then it is likely that there is an uninitialized variable somewhere.

  8. #8
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by robwhit View Post
    >Let me ask you a question: If your car breaks down, do walk into the mechanic's shop without it, and go: "Hey, my car's broken. Can you fix it for me?"
    I would. How would you get to the shop?
    Quote Originally Posted by laserlight View Post
    In the tow truck?
    Exactly. Either way, people don't normally go to the mechanic without the car, do they?

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by IceDane View Post
    Exactly. Either way, people don't normally go to the mechanic without the car, do they?
    Alernatively, you ask the mechanic to come to the car.

    But when it comes to code, it's usually easier to show the code to us here than it is to get one of us to come to the code.

    Another consideration would be to enable "numerical exceptions" - so that when you get the NaN result, it traps immediately, rather than keep on using it further in the calculations.

    And having bugs disappear when adding debug statements isn't entirely uncommon - it's even got a name, "Heisenbug" after Heisenbergs uncertainty theorem.

    --
    Mats
    Last edited by matsp; 02-03-2008 at 09:02 AM.
    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.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    11

    Thanks

    Hi guys,
    Thanks! Especially to brewbuck (who is the only one who actually gave me an answer...)
    Sorry for not posting the code, my program is HUGE and I didn't think any of you would like to read it all (also I don't think I can actually post it). The parameters are defined in different files and as you already know - I have no idea where the problem is, so I can't just post part of the code.
    Anyways - knowing that there might be an uninitialized parameter will probably help me find the problem.
    Thanks again.
    Liat.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, I actually gave a "valid" suggestion too, to enable math exceptions in your code to detect the cause of the NaN more immediately - and I've got another suggestion: Try to cut your code "in halfes" - that is, you stop halfway and check if the values are sane. If they are, the fault is in the other half of the calculation.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Printing Lines to .txt File
    By Programmer3922 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 12:45 PM
  3. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  4. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 07:10 PM
  5. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 03:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21