[Numpy-discussion] Portable functions for nans, signbit, etc.

Charles R Harris charlesr.harris at gmail.com
Wed Oct 1 22:41:31 EDT 2008


The normal signbit function of gcc without the -std=c99 flag doesn't work
correctly for nans and infs. I found the following code on a boost mailing
list and it might be helpful here for portability.

const boost::uint32_t signbit_mask
     = binary_cast<boost::uint32_t>(1.0f)
     ^ binary_cast<boost::uint32_t>(-1.0f);

inline bool signbit(float x)
{
     return binary_cast<boost::uint32_t>(x) & signbit_mask;
}

inline bool signbit(double x)
{
     return signbit(static_cast<float>(x));
}
inline bool signbit(long double x)
{
     return signbit(static_cast<float>(x));
}

Which is rather clever. I think binary_cast will require some pointer abuse.
There are a bunch of other boost functions here
<http://tinyurl.com/3tvj9u>that might prove useful. This file

 <goog_1222871357985>floating_point_utilities_v3.zip
<http://www.boostpro.com/vault/index.php?action=downloadfile&filename=floating_point_utilities_v3.zip&directory=Math%20-%20Numerics&PHPSESSID=64b789bb8092caa29b11839ec7526611>
portable isnan, fpclassify, signbit etc. + facets for portable handling of
infinity and NaN in text streams

Looks particularly interesting. It's a bit large for the mailing list so I
won't attach it. The Boost license should be compatible with numpy.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20081001/068efd7f/attachment.html>


More information about the NumPy-Discussion mailing list