|
Boost : |
From: Robert Ramey (ramey_at_[hidden])
Date: 2005-11-17 13:00:01
Troy S. and I have been looking at the question of implementing
serialization
of NaN, +/-Inf for floats, and doubles for portable text or binary archives.
Turns out there's a fly in the ointment.
For determining if a given double contains a "special" value, float.h
contains the following handy function
int _fpclass(
double x
);
which returns one of the following values:
_FPCLASS_SNAN Signaling NaN
_FPCLASS_QNAN Quiet NaN
_FPCLASS_NINF Negative infinity ( -INF)
_FPCLASS_NN Negative normalized non-zero
_FPCLASS_ND Negative denormalized
_FPCLASS_NZ Negative zero ( - 0)
_FPCLASS_PZ Positive 0 (+0)
_FPCLASS_PD Positive denormalized
_FPCLASS_PN Positive normalized non-zero
_FPCLASS_PINF Positive infinity (+INF)
So we could write a flag to the archive indicating if its a special value.
So far, so good.
When the archive is read back, we can read the flag and initialize
the variable with the appropriate value.
BUT - I can't find any "official" to initialize a float/double to any of
these
values. They seem to be the result of operations and its certainly
not obvious that all compilers would be on the same page here.
Note that this same problem arises whenever a float/double is written/read
to/from a stream in a way designed to be portable. So it must have come
up before. What's the solution here?
Robert Ramey
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk