Thread: double, NaN, and super small numbers

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    53

    double, NaN, and super small numbers

    Hi All,
    I need a quick answer, I have a function that returns a double, the formula becomes super small and eventually becomes less tan 10^-308, and then it returns NaN instead of 0, Once my function is ~10^-10 for my purposes is zero, How do I get it to return 0 if it's less than a certain value or if it is NaN I would like to to return 0. Thanks

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by nbo10 View Post
    Hi All,
    I need a quick answer, I have a function that returns a double, the formula becomes super small and eventually becomes less tan 10^-308, and then it returns NaN instead of 0, Once my function is ~10^-10 for my purposes is zero, How do I get it to return 0 if it's less than a certain value or if it is NaN I would like to to return 0. Thanks
    Just a little bit of rewording:
    Code:
    if (function value is less than 'certain value') or (function value is NaN)
       return 0;
    return function value;
    I mean, by asking the question, you've practically got the pseudo-code for it.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    53
    So how would I code to check if a double is NaN?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    float x;
    isnan(x);
    or in microsoft compiler:
    _isnan(x);
    --
    Mats

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    53
    Thanks I was trying x=='NaN' and it wasn't working.

    isnan works great. thanks

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I believe you can also use other tests like if (!(x==x)) to check for nans.
    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"

Popular pages Recent additions subscribe to a feed