简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM