how to check a double is inf or NaN?

Discussion in 'C++' started by zl2k, Jan 15, 2007.

  1. zl2k

    zl2k Guest

    hi,
    Can someone let me know the command to check if a value is inf or NaN
    in c++? I am using gcc in linux. Thanks for help.
    zl2k
    zl2k, Jan 15, 2007
    #1
    1. Advertising

  2. zl2k

    Ivan Novick Guest

    zl2k wrote:
    > Can someone let me know the command to check if a value is inf or NaN
    > in c++? I am using gcc in linux. Thanks for help.


    man isnan

    -----
    Ivan
    http://www.0x4849.net
    Ivan Novick, Jan 15, 2007
    #2
    1. Advertising

  3. zl2k

    Ondra Holub Guest

    zl2k napsal:
    > hi,
    > Can someone let me know the command to check if a value is inf or NaN
    > in c++? I am using gcc in linux. Thanks for help.
    > zl2k


    NaN is the only value, for which is expression value == value always
    false. So:

    template<typename T>
    inline bool isnan(T value)
    {
    return value != value;
    }

    // requires #include <limits>
    template<typename T>
    inline bool isinf(T value)
    {
    return std::numeric_limits<T>::has_infinity() &&
    value == std::numeric_limits<T>::infinity();
    }
    Ondra Holub, Jan 15, 2007
    #3
  4. zl2k

    Ondra Holub Guest

    Ondra Holub napsal:
    > zl2k napsal:
    > > hi,
    > > Can someone let me know the command to check if a value is inf or NaN
    > > in c++? I am using gcc in linux. Thanks for help.
    > > zl2k

    >
    > NaN is the only value, for which is expression value == value always
    > false. So:
    >
    > template<typename T>
    > inline bool isnan(T value)
    > {
    > return value != value;
    > }
    >
    > // requires #include <limits>
    > template<typename T>
    > inline bool isinf(T value)
    > {
    > return std::numeric_limits<T>::has_infinity() &&
    > value == std::numeric_limits<T>::infinity();
    > }


    Small correction of posted code (has_infinity is not function, so
    parentheses are removed):

    template<typename T>
    inline bool isnan(T value)
    {
    return value != value;

    }

    // requires #include <limits>
    template<typename T>
    inline bool isinf(T value)
    {
    return std::numeric_limits<T>::has_infinity &&
    value == std::numeric_limits<T>::infinity();
    }
    Ondra Holub, Jan 15, 2007
    #4
  5. zl2k

    Greg Guest

    Ondra Holub wrote:
    > Ondra Holub napsal:
    > > zl2k napsal:
    > > > hi,
    > > > Can someone let me know the command to check if a value is inf or NaN
    > > > in c++? I am using gcc in linux. Thanks for help.
    > > > zl2k

    > template<typename T>
    > inline bool isnan(T value)
    > {
    > return value != value;
    >
    > }
    >
    > // requires #include <limits>
    > template<typename T>
    > inline bool isinf(T value)
    > {
    > return std::numeric_limits<T>::has_infinity &&
    > value == std::numeric_limits<T>::infinity();
    > }


    Why not:

    #include <cmath>

    ...
    if ( std::isinf( value ))
    {
    // value is infinity
    }

    if ( std::isnan( value ))
    {
    // value is not a number
    }

    Greg
    Greg, Jan 15, 2007
    #5
  6. zl2k

    Greg Guest

    Ondra Holub wrote:
    > Ondra Holub napsal:
    > > zl2k napsal:
    > > > hi,
    > > > Can someone let me know the command to check if a value is inf or NaN
    > > > in c++? I am using gcc in linux. Thanks for help.
    > > > zl2k

    > >
    > > // requires #include <limits>
    > > template<typename T>
    > > inline bool isinf(T value)
    > > {
    > > return std::numeric_limits<T>::has_infinity() &&
    > > value == std::numeric_limits<T>::infinity();
    > > }


    This implementation of isinf() incorrectly returns false when value is
    equal to negative infinity.

    Greg
    Greg, Jan 15, 2007
    #6
  7. zl2k

    Ondra Holub Guest

    Greg napsal:
    > Ondra Holub wrote:
    > > Ondra Holub napsal:
    > > > zl2k napsal:
    > > > > hi,
    > > > > Can someone let me know the command to check if a value is inf or NaN
    > > > > in c++? I am using gcc in linux. Thanks for help.
    > > > > zl2k

    > > template<typename T>
    > > inline bool isnan(T value)
    > > {
    > > return value != value;
    > >
    > > }
    > >
    > > // requires #include <limits>
    > > template<typename T>
    > > inline bool isinf(T value)
    > > {
    > > return std::numeric_limits<T>::has_infinity &&
    > > value == std::numeric_limits<T>::infinity();
    > > }

    >
    > Why not:
    >
    > #include <cmath>
    >
    > ...
    > if ( std::isinf( value ))
    > {
    > // value is infinity
    > }
    >
    > if ( std::isnan( value ))
    > {
    > // value is not a number
    > }
    >
    > Greg


    isnan is part of C99 standard. It is not required in current C++
    standard (because it is from 1998). Although many compilers support it,
    it is not 100% portable. See
    http://www.parashift.com/c -faq-lite/newbie.html#faq-29.15
    Ondra Holub, Jan 16, 2007
    #7
  8. zl2k

    Ondra Holub Guest

    Greg napsal:
    > Ondra Holub wrote:
    > > Ondra Holub napsal:
    > > > zl2k napsal:
    > > > > hi,
    > > > > Can someone let me know the command to check if a value is inf or NaN
    > > > > in c++? I am using gcc in linux. Thanks for help.
    > > > > zl2k
    > > >
    > > > // requires #include <limits>
    > > > template<typename T>
    > > > inline bool isinf(T value)
    > > > {
    > > > return std::numeric_limits<T>::has_infinity() &&
    > > > value == std::numeric_limits<T>::infinity();
    > > > }

    >
    > This implementation of isinf() incorrectly returns false when value is
    > equal to negative infinity.
    >
    > Greg


    Question was, how to check for infinity (which I understand positive
    infinity as 1 is understand +1), not how to check for positive or
    negative infinity. Of course, it does not detect -inf. If there is
    necessary to detect any infinity, it may be done for example this way:

    #include <limits>
    template<typename T>
    inline bool isanyinf(T value)
    {
    return value >= std::numeric_limits<T>::min() && value <=
    std::numeric_limits<T>::max();
    }
    Ondra Holub, Jan 16, 2007
    #8
  9. zl2k

    Pete Becker Guest

    Ondra Holub wrote:
    >
    > isnan is part of C99 standard. It is not required in current C++
    > standard (because it is from 1998). Although many compilers support it,
    > it is not 100% portable. See


    The current C++ standard is from 2003, a technical revision of the 1998
    standard.

    isnan is part of TR1, and has been incorporated into the working draft
    for C++0x.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
    Pete Becker, Jan 16, 2007
    #9
  10. zl2k

    Greg Guest

    Ondra Holub wrote:
    > Greg napsal:
    > > Ondra Holub wrote:
    > > > Ondra Holub napsal:
    > > > > zl2k napsal:
    > > > > > hi,
    > > > > > Can someone let me know the command to check if a value is inf or NaN
    > > > > > in c++? I am using gcc in linux. Thanks for help.
    > > > > > zl2k
    > > > >
    > > > > // requires #include <limits>
    > > > > template<typename T>
    > > > > inline bool isinf(T value)
    > > > > {
    > > > > return std::numeric_limits<T>::has_infinity() &&
    > > > > value == std::numeric_limits<T>::infinity();
    > > > > }

    > >
    > > This implementation of isinf() incorrectly returns false when value is
    > > equal to negative infinity.

    >
    > Question was, how to check for infinity (which I understand positive
    > infinity as 1 is understand +1), not how to check for positive or
    > negative infinity.


    The standard routine, isinf() in <math.h> tests for whether its
    argument has an infinite value. Therefore isinf() returns true when
    called with either positive or negative infinity, because both are
    infinite values.

    Implementing another isinf() function that performs a similar, but
    different test, would just cause confusion - at best. A programmer
    would have to make sure which isinf() is being called in a particular
    fil - just to know what its return value means.

    Greg
    Greg, Jan 16, 2007
    #10
  11. zl2k

    zl2k Guest

    Greg wrote:
    > Ondra Holub wrote:
    > > Greg napsal:
    > > > Ondra Holub wrote:
    > > > > Ondra Holub napsal:
    > > > > > zl2k napsal:
    > > > > > > hi,
    > > > > > > Can someone let me know the command to check if a value is inf or NaN
    > > > > > > in c++? I am using gcc in linux. Thanks for help.
    > > > > > > zl2k
    > > > > >
    > > > > > // requires #include <limits>
    > > > > > template<typename T>
    > > > > > inline bool isinf(T value)
    > > > > > {
    > > > > > return std::numeric_limits<T>::has_infinity() &&
    > > > > > value == std::numeric_limits<T>::infinity();
    > > > > > }
    > > >
    > > > This implementation of isinf() incorrectly returns false when value is
    > > > equal to negative infinity.

    > >
    > > Question was, how to check for infinity (which I understand positive
    > > infinity as 1 is understand +1), not how to check for positive or
    > > negative infinity.

    >
    > The standard routine, isinf() in <math.h> tests for whether its
    > argument has an infinite value. Therefore isinf() returns true when
    > called with either positive or negative infinity, because both are
    > infinite values.
    >
    > Implementing another isinf() function that performs a similar, but
    > different test, would just cause confusion - at best. A programmer
    > would have to make sure which isinf() is being called in a particular
    > fil - just to know what its return value means.
    >
    > Greg


    Thanks for all the comments, very helpful.

    zl2k
    zl2k, Jan 17, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hans
    Replies:
    3
    Views:
    2,456
    Ryan Stewart
    May 16, 2004
  2. Jon Wilson

    NaN and inf literal constants g++

    Jon Wilson, Dec 31, 2004, in forum: C++
    Replies:
    3
    Views:
    10,669
    Jon Wilson
    Dec 31, 2004
  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    checking double for Inf or NaN - how?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, May 18, 2006, in forum: C Programming
    Replies:
    55
    Views:
    2,496
    Keith Thompson
    Jun 3, 2006
  4. Joshua J. Kugler
    Replies:
    4
    Views:
    443
    Grant Edwards
    Sep 21, 2007
  5. ardi
    Replies:
    14
    Views:
    210
    Tim Prince
    Jan 5, 2014
Loading...

Share This Page