Boost logo

Boost Users :

Subject: Re: [Boost-users] min and max preferring NaN
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2011-03-09 16:44:22


AMDG

On 03/08/2011 11:03 AM, Rhys Ulerich wrote:
> The usual std::min and std::max prefer numbers over NaNs per
> http://en.wikipedia.org/wiki/IEEE_754_revision#min_and_max. POSIX's fmin(3)
> does as well.
>
> Can anyone suggest a better implementation (chained ternary complaints aside)
> for a NaN-preferring min or max than brute force things akin to
>
> template<class T>
> inline
> const T& minnan(const T& a, const T& b)
> {
> return UNLIKELY((boost::math::isnan)(a)) ? a :
> (a< b) ? a : b;
> }
>
> where UNLIKELY is a small macro telling the compiler to generate code expecting
> that the boost::math::isnan test fails?
>
> The need arises in a numerical simulation where I want to gradually accumulate
> a global minimum where throwing away NaNs ("windowing" in IEEE 754
> revision-speak) is the unacceptable.

How about:

template<class T>
inline const T& minnan(const T& a, const T& b)
{
     return !(b <= a)? a : b;
}

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net