Is there a C++ Standards compliant way to determining the structure of a 'float', 'double', and 'long double' at compile-time ( or run-time, as an alternative )?
If I assume std::numeric_limits< T >::is_iec559 == true
and std::numeric_limits< T >::radix == 2
, I suspect the is possible by the following rules:
- first X-bits are the significand.
- next Y-bits are the exponent.
- last 1-bit is the sign-bit.
with the following expressions vaguely like:
size_t num_significand_bits = std::numeric_limits< T >::digits;
size_t num_exponent_bits = log2( 2 * std::numeric_limits< T >::max_exponent );
size_t num_sign_bits = 1u;
except I know
std::numeric_limits< T >::digits
includes the "integer bit", whether or not the format actually explicitly represents it, so I don't know how to programmatically detect and adjust for this.- I'm guessing
std::numeric_limits< T >::max_exponent
is always2^(num_exponent_bits)/2
.
Background: I'm trying to overcome two issues portably:
- set/get which bits are in the significand.
- determine where the end of 'long double' is so I know not to read the implicit padding bits that'll have uninitialized memory.