Boost logo

Boost :

From: John Maddock (john_at_[hidden])
Date: 2005-12-02 05:50:39


> I guess you are referring to the C99 function double nan(const char
> *tagp) here. C99 doesn't specify any particular semantics for tagp
> however, but
> at least among IEEE-754 platforms, is there much variation? I just
> tried playing around with this on g++ 3.4, and nan("str") appears to
> return 0x7FF8000000000000 + strtol("str") (as long as it is in range).

Sensible, but the meaning of the "str" part is implementation defined
according to C99, so there's still not much you could rely on in portable
code.

> But I can't seem to find any sensible way of reading off what the nan
> payload actually is. Printf() with %f or %F appears to allow an
> output of "NAN*" where the "*" is unspecified. But g++ doesn't
> actually do it, it
> just prints "nan" or "NAN", irrespective of the payload. I would have
> expected it would be symmetric with strtod, which allows notation
> strtod("NAN(str)") and is equivalent to nan("str") (that, at least, is
> C99). Ugh!
>
> Are there any floating point formats in use that allow NaN's, other
> than IEEE-754? It might be easier to just ignore the existing
> functions that
> have inconsistent behaviour and write our own.

Sigh, it depends on how many formats there are, and how much configuration
you're prepared to engage in, float and double are probably manageable, the
problems generally start with long long (Darwin's strange long double is a
"pair of doubles" for example).

I guess the obvious interfaces would be something like:

Real nan(IntType payload);
IntType payload(Real nan);

John.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk