min and max preferring NaN

'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

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

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

Can anyone suggest a better implementation ... for a NaN-preferring min or max ...
template < class T > inline const T& minnan( const T& a, const T& b ) { return a < b || is_nan(a) ? a : b; }
That's much cleaner. Thank you Kris. I've tossed my goofy UNLIKELY macro around the is_nan check just because. It's interesting that when using this minnan() implementation on a sequence of values that short circuiting suggests using the "incumbent" minimum value as the first parameter and the "challenger" candidate value as the second parameter. - Rhys

2011/3/10 Rhys Ulerich
inline const T& minnan( const T& a, const T& b ) { template < class T > return a < b || is_nan(a) ? a : b; }
That's much cleaner. Thank you Kris. I've tossed my goofy UNLIKELY macro around the is_nan check just because.
It's interesting that when using this minnan() implementation on a sequence of values that short circuiting suggests using the "incumbent" minimum value as the first parameter and the "challenger" candidate value as the second parameter.
I'm no expert in this, but I'd expect a modern compiler to generate for a modern procesor such code, that would check both conditions parallel somehow, i.e. in different instruction pipelines. So I wouldn't expect UNLIKELY or short circuiting to make a defference.. I'd expect UNLIKELY to kick-in well in situations, the conditional branches are larger. And I'd expect short circuiting to make a defference in situations, where the calculations of the condithis are large (or have sideeffects). But only measurements can tell for sure ;-) Regards, Kris
participants (3)
-
Krzysztof Czainski
-
Rhys Ulerich
-
Steven Watanabe