Boost logo

Boost Users :

Subject: [Boost-users] min and max preferring NaN
From: Rhys Ulerich (rhys.ulerich_at_[hidden])
Date: 2011-03-08 14:03:07


'Afternoon,

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.

Thanks,
Rhys


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