Reputation: 427
32 Bit Standard:
1 Bit for Positive/Negative value of the number. 8 Bits for the Exponent and 24 Bits for Mantisse.
8 Bits for Exponent, that means 1 * 2^7 + 1 * 2^6 + ... = 255 When the maximum Exponent is 127, then the minimum Exponent should be -128, so that 126 + 128 = 255.
But why is Java saying that the minimum Exponent is -126? 255 - (127+126)= 2, so there are two numbers which we are not using.
Upvotes: 2
Views: 810
Reputation: 33
There are 2 exponent sequences that encode special values. All 0s encodes either 0 or subnormals, depending on the mantissa. All 1s encodes either Infinity or NaNs. This means instead of 256 exponent sequences, there are, like you said, 254 sequences to encode normal numbers.
Thus, it makes sense that exponent 00000001 encodes power -126 and 11111110 encodes power 127. This is the exponent range for normal numbers.
https://en.wikipedia.org/wiki/Single-precision_floating-point_format
Upvotes: 3
Reputation: 102902
That number has a 'bias', whatever is in those bits? First subtract 0x7F
from it to get your value. The lowest exponent is reached by using value 0x01
: 0x01 - 0x7F = 1 - 127 = -126
. The highest is reached with value 0xFE
: 0xFE - 0x7F = 254 - 127 = 127
.
But, what happened to exponent values 0x00
and 0xFF
? That's why there are 254 and not 256 unique exponents available: Those two are special magic and not available normally. Exponent 0 is both used to encode 0 (if the number for the fraction is also 0), or for so-called subnormal numbers, which are numbers extremely close to 0.
0xFF
is used for special values; this is how floats can store NaN
and both infinities.
Upvotes: 4