I am using NaNs with payloads (so that the mantissa contains important information but is still treated as a NaN). For example one such value may be represented using IEEE-754-1985 in hex as FFF8000055550001
which has a sign bit of 1, the exponent of 7FF
for a NaN/infinity, the quiet NaN bit set (at least on most architectures), and a payload of 0x55550001
.
However this has a few problems. First, this cannot be created easily as a literal in C/C++ since the common methods all cannot be used to initialize literals:
- hex-literal notation for doubles (the best option, see here) but only seems to support
finite values and not infinities or NaNsnon-NaN values (I can getinf
s by usingp1024
andp-1024
but the mantissa is ignored in this case) memcpy
is nice as it avoids aliasing but requires a function callreinterpret_cast
is C++ only (which is okay) but requires the operand to be a variable or pointer and not a literal- union-type-punning but I don't think this can be used to initialize a static-time constant
Is there any method to setup the static constant for a NaN with a payload? It may be assumed that the system is IEEE-754-1985 compliant and that long
s and double
s have the same endian-ness.