How does one catch NaN type errors?

This is a discussion on How does one catch NaN type errors? within the C++ Programming forums, part of the General Programming Boards category; Is there a good way to catch all the NaN type errors like: 1.#IND -1.#IND -1.#INF 1.#INF etc.... I can't ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    6

    How does one catch NaN type errors?

    Is there a good way to catch all the NaN type errors like:
    • 1.#IND
    • -1.#IND
    • -1.#INF
    • 1.#INF
    • etc....


    I can't do:
    Code:
    if (var == -1.#IND)
    because the # calls up compiler errors.

    Anyhow, i'm looking for a suggestion for a catchall function for numerical explosion errors that returns 0 if it's a normal double and 1 otherwise. Hope you can help!

    Thanks!
    Last edited by testing123; 08-16-2006 at 11:39 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,853
    Somewhere in math.h (or is it float.h - check your compiler), there is a function called isNan(), which you pass your suspect result to, and it returns true or false.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    Someone just wrote me that those functions are a C99 nonstandard function. I have no clue how to enable those. Any simple way to code these functions?

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    float var = 1.0/0.0;
    if ( var != var )
    should work.
    Kurt

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    Doesn't work. The compiler won't let me divide by zero.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    Really I'm looking for the finite function. That way I can get a catch-all

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,853
    > those functions are a C99 nonstandard function.
    Odd, I thought they were a C99 standard function.

    There are two C standards, C89 which everyone should be able to do and C99 which only a few compilers are edging towards full compliance.

    But anyway, many compilers have all sorts of extensions for all sorts of things, so you may still have something you can use.

    I've no idea what ZuK was up to with a divide by zero?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    He wanted a NaN.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,853
    It seems to me the OP wants isnormal() to exclude all the NaN's, indefinites and infinities.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Perhaps:
    Code:
    if (var != numeric_limits<double>::quiet_NaN())
    http://www.dinkumware.com/manuals/?m...its::quiet_NaN
    Last edited by Daved; 08-16-2006 at 12:45 PM.

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    Quote Originally Posted by Salem
    It seems to me the OP wants isnormal() to exclude all the NaN's, indefinites and infinities.
    Correct! That's what I want

  12. #12
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The compiler is smart enough to refuse to compile my original example
    Code:
    #include <iostream>
    
    int main() {
       float f1 = 0;
       float f2 = f1/0;
       if ( f2 != f2 ) {
           std::cout << "error dividing. " << f2 << std::endl;
       }   
    }
    my output
    Code:
    dz.cc: In function `int main()':
    dz.cc:5: Warnung: division by zero in `f1 / 0'
    kurt@power:~$ ./a.out
    error dividing. nan
    Kurt

  13. #13
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Ah, but taking in user input is a good way to get bad numbers... those users and their wacky ways.

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    The compiler is smart enough to refuse to compile my original example
    Hmm... it's somewhat debatable if that is a good thing, unless you have your compiler set to stop on warnings.

    http://www.jdl.co.uk/briefings/divByZeroInCpp.html
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Did a little experimenting now.
    comparing a var with itself compares false for nan only.
    inf compares true.
    @Mario F.: you are right it's just a warning.
    Kurt

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 09:51 AM
  2. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 05:33 PM
  3. Cant find the error
    By Coder87C in forum C Programming
    Replies: 8
    Last Post: 06-19-2005, 02:57 AM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 01:22 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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