From: Paul A Bristow (pbristow_at_[hidden])
Date: 2006-08-02 13:43:00
| -----Original Message-----
| From: boost-bounces_at_[hidden]
| [mailto:boost-bounces_at_[hidden]] On Behalf Of Johan Råde
| Sent: 02 August 2006 16:08
| To: boost_at_[hidden]
| 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
| > 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
| 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 A Bristow Prizet Farmhouse, Kendal, Cumbria UK LA8 8AB +44 1539561830 & SMS, Mobile +44 7714 330204 & SMS pbristow_at_[hidden]
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk