how to check a double is inf or NaN?

Z

zl2k

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
 
O

Ondra Holub

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();
}
 
O

Ondra Holub

Ondra Holub napsal:
zl2k napsal:

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();
}
 
G

Greg

Ondra said:
Ondra Holub napsal:
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
 
O

Ondra Holub

Greg napsal:
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();
}
 
P

Pete Becker

Ondra said:
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)
 
G

Greg

Ondra said:
Greg napsal:

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
 
Z

zl2k

Greg said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top