Boost logo

Boost :

From: Johan Råde (rade_at_[hidden])
Date: 2006-07-26 07:49:48


Joe Gottman wrote:
> Several libraries have an is_nan() function buried deep in the code. Are
> there any plans to make a general is_nan function available? This would be
> quite useful, as well as similar functions like is_finite.
>
> Joe Gottman
>
>
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
>

The date_time library contains the following is_nan function:

static bool is_nan(const T& x)
{
     return std::numeric_limits<T>::has_quiet_NaN && (x != x);
}

It has been pointed out by Martin Bonner that this is not safe,
for it requires strict IEEE 754 compliance and it may be optimized away
by an over-eager optimizing compiler.

Here is the implementation I'm currently using:

//-------------------------------------------------------------------

#if defined(BOOST_MSVC) // Microsoft Visual C++
        
template<class T> int is_nan(T t)
{
     return _isnan(t);
}

#elif defined(__GNUC__) // GCC

template<class T> int is_nan(T t)
{
     return isnan(t);
}

#else // generic

template<class T> int is_nan(T t)
{
     if(std::numeric_limits<T>::has_infinity)
         return !(t <= std::numeric_limits<T>::infinity());
     else
         return t != t;
}

#endif

//--------------------------------------------------------------------

Comments?
Anyone wants to add more #if's?

--Johan Råde


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk