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.
2011/3/9 Steven Watanabe <watanabesj@gmail.com>
How about:
template<class T>
inline const T& minnan(const T& a, const T& b)
{
return !(b <= a)? a : b;
}
What if b is NaN, and a is a number?
Anyway, how about:
template < class T >
inline const %& minnan( const T& a, const T& b )
{
return a < b || is_nan(a) ? a : b;
}
Regards,
Kris