Boost logo

Boost :

From: Johan Råde (rade_at_[hidden])
Date: 2006-07-25 05:27:02


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
>

Floating point numbers can be classified into four categories:

1. finite numbers
2. infinity
3. negative infinity
4. not-a-number (nan)

So maybe we need functions:

template<class T> bool is_finite(T a)
{
        return a >= -std::numeric_limits<T>::min()
                && a <= std::numeric_limits<T>::max();
}

template<class T> bool is_infinity(T a)
{
        return std::numeric_limits<T>::has_infinity
                && a == std::numeric_limits<T>::infinity();
}

template<class T> bool is_nan(T a)
{
        return a != a;
}

You test for negative infinity by calling is_infinity(-a).

Three issues:

1. Do these implementations work an all platforms?

2. The category not-a-number can be divided into the sub-categories
quite not-a-number and signalling not-a-number. Does anyone need to
distinguish between them?

3. Are these the most efficient implementations. (Important if you have
a large vector of numbers and need to verify that each element is finite.)

--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