简体   繁体   English

为什么 Java 中浮点数(32 位)的指数是 -126 而不是 -128?

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

32 Bit Standard: 32 位标准:

1 Bit for Positive/Negative value of the number. 1 位用于数字的正/负值。 8 Bits for the Exponent and 24 Bits for Mantisse.指数为 8 位,尾数为 24 位。

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. Exponent 8 位,即 1 * 2^7 + 1 * 2^6 +... = 255 当最大 Exponent 为 127 时,最小 Exponent 应为 -128,因此 126 + 128 = 255。

But why is Java saying that the minimum Exponent is -126?但是为什么 Java 说最小指数是-126? 255 - (127+126)= 2, so there are two numbers which we are not using. 255 - (127+126)= 2,所以我们没有使用两个数字。

That number has a 'bias', whatever is in those bits?这个数字有一个“偏见”,这些位是什么? First subtract 0x7F from it to get your value.首先从中减去0x7F以获得您的值。 The lowest exponent is reached by using value 0x01 : 0x01 - 0x7F = 1 - 127 = -126 .使用值0x01达到最低指数: 0x01 - 0x7F = 1 - 127 = -126 The highest is reached with value 0xFE : 0xFE - 0x7F = 254 - 127 = 127 .最高值达到0xFE : 0xFE - 0x7F = 254 - 127 = 127

But, what happened to exponent values 0x00 and 0xFF ?但是,指数值0x000xFF发生了什么? That's why there are 254 and not 256 unique exponents available: Those two are special magic and not available normally.这就是为什么有 254 个而不是 256 个可用的唯一指数:这两个是特殊的魔法,通常不可用。 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.指数 0 既用于编码 0(如果分数的数字也是 0),也用于编码所谓的次正规数,即非常接近 0 的数字。

0xFF is used for special values; 0xFF用于特殊值; this is how floats can store NaN and both infinities.这就是浮点数如何存储NaN和两个无穷大。

There are 2 exponent sequences that encode special values.有 2 个指数序列编码特殊值。 All 0s encodes either 0 or subnormals, depending on the mantissa.所有 0 编码 0 或次正规,具体取决于尾数。 All 1s encodes either Infinity or NaNs.所有 1 都编码 Infinity 或 NaN。 This means instead of 256 exponent sequences, there are, like you said, 254 sequences to encode normal numbers.这意味着就像你说的那样,有 254 个序列来编码正常数字,而不是 256 个指数序列。

Thus, it makes sense that exponent 00000001 encodes power -126 and 11111110 encodes power 127. This is the exponent range for normal numbers.因此,指数 00000001 编码幂 -126 和 11111110 编码幂 127 是有意义的。这是正常数字的指数范围。

https://en.wikipedia.org/wiki/Single-precision_floating-point_format https://en.wikipedia.org/wiki/Single-precision_floating-point_format

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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