0

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.

John Kugelman
  • 307,513
  • 65
  • 473
  • 519
Optimal
  • 1
  • 1
  • 1
    Java `float` is just standard IEEE 754 single precision floating point numbers. This [wiki page](https://en.wikipedia.org/wiki/Single-precision_floating-point_format) details the format specification. I do not have the time to excerpt relevant part to compose an answer, so you may need to go find it yourself. – glee8e Nov 02 '19 at 13:39
  • @Optimal _"why is Java saying that the minimum Exponent is -126?"_ Please provide the link to the page where this is documented. – Robert Nov 02 '19 at 13:52
  • Here: https://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2.3 – Gustavo Passini Nov 02 '19 at 13:58

2 Answers2

2

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.

John Kugelman
  • 307,513
  • 65
  • 473
  • 519
rzwitserloot
  • 44,252
  • 4
  • 27
  • 37
  • Why are the subnormal numbers “so-called” subnormal numbers? Are they not in fact less than the normal numbers? Are you expressing doubt about whether they are in fact subnormal numbers? – Eric Postpischil Nov 02 '19 at 17:14
  • @EricPostpischil: I think that's "so-called" in the sense of introducing a term, rather than in the sense of casting doubt on the appropriateness of that term. https://www.merriam-webster.com/dictionary/so-called – Mark Dickinson Nov 03 '19 at 17:52
  • @MarkDickinson: Indeed, but it is not good to use an ambiguous term that may be pejorative. It would be better to simply put it in italics to convey it is a technical term that is being introduced. – Eric Postpischil Nov 03 '19 at 18:44
  • I addressed many of your questions in https://stackoverflow.com/a/58924829/1766831 – Rick James Nov 19 '19 at 00:51
0

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

Anna Song
  • 3
  • 2
  • Could you explain the last sentence _Thus, it makes sense that exponent 00000001 encodes power -126 and 11111110 encodes power 127. This is the exponent range for normal numbers._? How do you encode [0...254] values as [-126...127] ? – snr Dec 21 '20 at 06:11
  • @snr, My answer was in response to the original question, which was why there are 2 sequences that don't seem to be used. Your question is how the exponent is decoded, which is completely different. The short answer is that an offset of -127 needs to be applied to decode the exponent. A more detailed explanation can be found on the wiki page I linked. – Anna Song Dec 21 '20 at 22:13