javafloating-pointieee-754single-precision

Why is the Exponent for Float (32 Bit) in Java -126 and not -128?


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.


Solution

  • 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.