
| -----Original Message----- | From: boost-bounces@lists.boost.org | [mailto:boost-bounces@lists.boost.org] On Behalf Of Johan RĂ¥de | Sent: 02 August 2006 16:08 | To: boost@lists.boost.org | Subject: Re: [boost] Portable signbit macro/function? | | 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; | } Sneaky - and looks plausible - but may be quicker to use a macro, if available, as I suspect it is for many platforms. Need to check it works for negative zero and NaNs too - I may get a moment to check this out, for my sins - since I raised the issue ;-) Paul --- Paul A Bristow Prizet Farmhouse, Kendal, Cumbria UK LA8 8AB +44 1539561830 & SMS, Mobile +44 7714 330204 & SMS pbristow@hetp.u-net.com