Boost logo

Boost :

From: Gennaro Prota (gennaro_prota_at_[hidden])
Date: 2006-08-02 14:21:13

On Wed, 2 Aug 2006 16:53:12 +0100, "John Maddock"
<john_at_[hidden]> wrote:

>Paul A Bristow wrote:
>> Does anyone have a ***portable*** version of macro signbit (C99)
>Not quite, but I recently added a rather brain dead version to the
>Math-Toolkit code, just a:
>template <class T>
>int signbit(const T& v)
>{ return v < 0 ? 1 : 0; }
>To be honest that's not very useful :-(
>What I do find more useful is:
>template <class T>
>int sign(const T& v)
>{ return v < 0 ? -1 : 1; }
>which is invaluable when you need to compare the sign of two (or more)
>numbers but are concerned about numeric overflow/underflow in the usual a*b
>< 0 trick.

TR1 asks for a non-macro version:

  template<class T> bool signbit(T x);

Like its C99 counterpart (which formally yields an int, but the
behavior is bool-like: nonzero iff the argument value has negative
sign) it shall work for any floating point value, including, if
supported, signed zeros, infinities and NaNs. I didn't try but I guess
the only slightly tricky point to watch for is signaling NaNs.

Note that all this is quite different, in scope and semantics, from
your sign facility, which BTW could perhaps be "extended" to:

  template <class T>
  int sign(const T& v)
  { return v < 0 ? -1 : (v > 0 ? 1 : 0) }

[ Gennaro Prota, C++ developer for hire ]

Boost list run by bdawes at, gregod at, cpdaniel at, john at