IEEE floating point exponents are stored as unsigned integers, using a pre-defined exponent bias to offset the exponent.
The exponent bias seems to be consistently equal to numeric_limits<T>::max_exponent - 1
where T
is the floating point type.
I do not have any documentation that states this is always true, however, and I certainly don’t have any idea on non-IEEE floating point formats.
This must be known for functions like:
frexp
ilogb
logb
Does c++ have a specification for it, or must I assume numeric_limits<T>::max_exponent - 1
?
C++ does not define the bias, and you do not need to know it to use frexp
, ilogb
, or logb
. Those functions all use and return a mathematical exponent, not a biased exponent. (However, for frexp
, the exponent is scaled such that the significand is in [1/2, 1) rather than IEEE-754’s usual [1, 2).)1
You only need the bias if you are tinkering with the internal representation of the float, in which case your code is implementation-dependent. IEEE-754 defines the bias to be 2k−p−1−1, where k is the storage width in bits (such as 32 or 64) and p is the precision in bits (the number of bits in the mathematical significand, such as 24 or 53 for the common float
and double
types, which is one more than the width of the field that contains the primary encoding of the significand). Thus, for the common 32-bit format, the bias is 232−24−1−1 = 27−1 = 127. C++ implementations may use non-IEEE-754 formats.
1 If the exponent differs for frexp
and ilogb
/logb
, what does it mean to say it is mathematical or biased? Clearly there is some base point relative to which the exponent is measured, so how can it be unbiased? For frexp
, ilogb
, or logb
, the result for each function depends solely on the value of the number. You will get the same result regardless of whether you use the float
or double
variant of frexp
. Only the mathematical value matters. In contrast, when you look at the internal representation of a floating-point value, the exponent will differ depending on the format of the data; float
has a different bias than double
does.
I think you'll have to make an assumption here.
In particular, is_iec559
could be false
, in which case it's possible (however unlikely) that the exponent will be represented entirely differently (e.g., as a 2's complement instead of a biased integer).
A different representation of the exponent really is quite unlikely though. Even VAX and IBM mainframe formats, although decidedly different from IEEE in other respects, still use excess-N format for their exponents.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With