Boost logo

Boost Users :

Subject: Re: [Boost-users] min and max preferring NaN
From: Krzysztof Czainski (1czajnik_at_[hidden])
Date: 2011-03-09 17:02:58


>
> 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_at_[hidden]>

> 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



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