|
Boost : |
From: Johan Råde (rade_at_[hidden])
Date: 2006-08-02 11:07:36
Paul A Bristow wrote:
> Does anyone have a ***portable*** version of macro signbit (C99)
>
> or can we combine versions that work on each compiler/hardware/endian...
>
> or an equivalent C++ function, perhaps called is_negative?
>
> (This would just test the signbit for the type(s)).
>
> There are several places where this would be useful in dealing properly with
> NaNs and zero and infinity etc.
>
> Thanks
>
> Paul
>
> PS Must be Boost license of course.
How about
bool is_negative(float x)
{
const float one = 1;
const float neg_one = -1;
const int one_bits = reinterpret_cast<const int&>(one);
const int neg_one_bits = reinterpret_cast<const int&>(neg_one);
const int sign_mask = one_bits ^ neg_one_bits;
return reinterpret_cast<int&>(x) & sign_mask;
}
--Johan Råde
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk