简体   繁体   English

为什么float.MaxValue 0xFF 0xFF 0x7F 0x7F而不是0x7F,0xFF,0xFF,0xFF就像整数?

[英]Why is float.MaxValue 0xFF 0xFF 0x7F 0x7F and not 0x7F, 0xFF, 0xFF, 0xFF like ints?

I guess this needs little extra information. 我想这需要额外的信息。 But, why is it that way? 但是,为什么会那样?

(This assumes the 23 bit float type is base on IEEE 754 32 bit binary floating point, and Little Endianness.) (这假设23位浮点类型基于IEEE 754 32位二进制浮点和Little Endianness。)

In 32bit ints it's pretty straightforward, since: 在32位整数中,它非常简单,因为:

int.MaxValue = 0x7FFFFFFF = 01111111 11111111 11111111 11111111 = 2147483647 int.MaxValue = 0x7FFFFFFF = 01111111 11111111 11111111 11111111 = 2147483647

In floats: 在浮点数:

0x7FFFFFFF = 01111111 11111111 11111111 11111111 = 2.14748365E+9 0x7FFFFFFF = 01111111 11111111 11111111 11111111 = 2.14748365E + 9

I get the E+9 notation. 我得到了E + 9表示法。 But why is it that: 但为什么会这样:

float.MaxValue = 0xFFFF7F7F = 11111111 11111111 01111111 01111111 = 3.40282347E+38 float.MaxValue = 0xFFFF7F7F = 11111111 11111111 01111111 01111111 = 3.40282347E + 38

You said: 你说:

int.MaxValue = 0x7F, 0xFF, 0xFF, 0xFF = 01111111 11111111 11111111 11111111 = 2147483647 int.MaxValue = 0x7F,0xFF,0xFF,0xFF = 01111111 11111111 11111111 11111111 = 2147483647

int.MaxValue is indeed 2147483647 because this is a 32-bit value, signed and encoded in two's complement , which is valued this way in your case: int.MaxValue确实是2147483647,因为这是一个32位值,以二进制补码进行签名和编码,在您的情况下以这种方式评估:

value = -2 31 b 31 + b 30 •2 30 + b 29 •2 29 + … + b 0 •2 0 = 2147483647 (as all bits b 0 to b 30 are 1 and b 31 is 0). 值= -2 31 b 31 + b 30 •2 30 + b 29 •2 29 + ... + b 0 •2 0 = 2147483647(因为所有位b 0至b 30均为1且b 31为0)。

You said: 你说:

In floats: 0x7F, 0xFF, 0xFF, 0xFF = 01111111 11111111 11111111 11111111 = 2.14748365E+9 在浮点数:0x7F,0xFF,0xFF,0xFF = 01111111 11111111 11111111 11111111 = 2.14748365E + 9

That's slightly incorrect. 这有点不正确。 What you've done is convert int.MaxValue to float (you've not interpreted the encoding of the max int value as float — you've changed representations) which is: 你所做的是将int.MaxValue转换为float(你没有将max int值的编码解释为float - 你已经改变了表示),这是:

2.14748365E+9 ≈ 2147483647 = 2147483647.0 — all the same stuff for humans, but floating-point values and integers are encoded in memory differently 2.14748365E +9≈2147483647= 2147483647.0 - 人类所有相同的东西,但浮点值和整数在内存中的编码方式不同

but

2147483647.0's hex representation (when rounded to a float) is 0x4f000000 not 0x7F, 0xFF, 0xFF, 0xFF. 2147483647.0的十六进制表示(舍入为浮点数时)为0x4f000000而不是0x7F,0xFF,0xFF,0xFF。

Here is why ( single precision floating point format ): 这就是为什么( 单精度浮点格式 ):

在此输入图像描述

0x4f000000 is valued as (-1) 0 •(1+0)•2 158-127 = 1•1•2 31 = 2 31 = 2147483648.0 0x4f000000的值为(-1) 0 •(1 + 0)•2 158-127 = 1•1•2 31 = 2 31 = 2147483648.0

You may check yourself here online IEE754 converter . 您可以在线查看IEE754转换器

You also said: 你还说:

float.MaxValue = 0xFF 0xFF 0x7F 0x7F = 11111111 11111111 01111111 01111111 = 3.40282347E+38 float.MaxValue = 0xFF 0xFF 0x7F 0x7F = 11111111 11111111 01111111 01111111 = 3.40282347E + 38

The 3.40282347E+38 value is correct, but its hex representation is not 0xFF 0xFF 0x7F 0x7F but 0x7f7fffff. 3.40282347E + 38值是正确的,但其十六进制表示不是0xFF 0xFF 0x7F 0x7F而是0x7f7fffff。

You may decode 0x7f7fffff this way: 您可以这样解码0x7f7fffff:

(-1) 0 •(1+2 -1 +2 -2 +2 -3 +…+2 -23 )•2 254-127 = 1•(1+1)•2 127 , which is approximately 2•2 127 = 2 128 ≈ 3.40282347E+38. (-1) 0 •(1 + 2 -1 +2 -2 +2 -3 + ... + 2 -23 )•2 254-127 = 1•(1 + 1)•2 127 ,约为2•2 127 = 2 128≈3.40282347E + 38。

You may wonder why the exponent is 254 and not 255. Exponent value 255 is a special case, and values with the exponent set to 255 are treated as +infinity or -infinity (depending on sign bit) if the significand (fraction) field is zero and as NaNs if the significand field is not zero. 你可能想知道为什么指数是254而不是255.指数值255是一种特殊情况,指数设置为255的值被视为+无穷大或-infinity(取决于符号位),如果有效数(分数)字段是如果有效数字段不为零,则为零和NaN。

This assumes the 32 bit float type is base on IEEE 754 32 bit binary floating point. 这假设32位浮点类型基于IEEE 754 32位二进制浮点。

The largest finite float has zero in both the sign bit and the least significant exponent bit. 最大有限浮点数在符号位和最低有效指数位中均为零。 In big-endian hex that is 0x7f7fffff. 在大端十六进制中,即0x7f7fffff。 The sign bit, the most significant bit of the number, is zero to make it positive. 符号位(数字的最高位)为零,使其为正。 The least significant exponent bit, the most significant bit of the second byte, is zero to get a finite number. 最低有效指数位(第二个字节的最高有效位)为零以获得有限数。 All exponent bits one is a NaN or infinity. 所有指数位1都是NaN或无穷大。

0xffff7f7f is the little-endian representation. 0xffff7f7f是little-endian表示。

It looks like your endianness is confused. 看起来你的字节顺序很混乱。 The maximum single precision (32-bit) IEEE-754 float value is composed of: 最大单精度(32位) IEEE-754浮点值由以下部分组成:

  • one sign bit, which is zero for positive numbers 一个符号位,正数为零
  • eight bits of exponent, which cannot be all ones (since that would mean the value was NaN), so the maximum value is 11111110 = 254 (biased) = 127 (actual exponent) 8位指数,不能全是1(因为那意味着值是NaN),所以最大值是11111110 = 254(偏差)= 127(实际指数)
  • 23 bits of mantissa, of which the maximum value would be all ones 23位尾数,其中最大值为全1

So I would expect the maximum single-precision float value to look like 0x7F7FFFFF. 所以我希望最大单精度浮点值看起来像0x7F7FFFFF。

Floating point data formats are quite different from integer formats. 浮点数据格式与整数格式完全不同。 The value is made up of three components, the sign, the exponent and the significand. 该值由三个组成部分组成,即符号,指数和有效数。 And these individual components are spread across multiple bytes within the data format. 这些单独的组件分布在数据格式的多个字节中。 For example, the sign is a single bit, and is stored in the same byte as another of the components. 例如,符号是单个位,并且与另一个组件存储在相同的字节中。

The bottom line is that what you know about integer representation is not applicable to floating point representation. 底线是您对整数表示的了解不适用于浮点表示。

Floats and floating point numbers in general are a little different than ints. 浮点数和浮点数通常与整数略有不同。 Whereas ints is just a two's compliment representation of an integer value, a floating point number consists of different parts: a sign bit (S), the exponent field (E), and the significand or mantissa (M), from left to right 虽然整数只是一个整数值的两个补码表示,浮点数由不同的部分组成:符号位(S),指数字段(E)和有效数字或尾数(M),从左到右

The maximum usable exponent is 0xFE, because 0xFF is a special value indicating an infinity. 最大可用指数为0xFE,因为0xFF是表示无穷大的特殊值。

So we have a sign bit that is 0 for positive numbers, a maximal exponent of 0xFE, and all 1's for the mantissa, resulting in 所以我们有一个符号位,对于正数为0,最大指数为0xFE,尾数为1,导致

SEEEEEEE EMMMMMMM MMMMMMMM MMMMMMMM

01111111 01111111 11111111 11111111 or 7F 7F FF FF 01111111 01111111 11111111 111111117F 7F FF FF

And since in a little endian machine, the order of the bytes is reversed, what you see is 因为在一个小端机器中,字节的顺序是相反的,你看到的是

FF FF 7F 7F

For IEEE 754-1985 spec, float point is in format of S8.24. 对于IEEE 754-1985规范,浮点格式为S8.24。
So the Largest normalized number is 2^127*(2-2^-23). 所以最大的归一化数是2 ^ 127 *(2-2 ^ -23)。
When encoded in binary, results in 0,1111 1110,111 1111 1111 1111 1111 1111 . 当以二进制编码时,得到0,1111 1110,111 1111 1111 1111 1111 1111
Please find more detailed in the IEEE 754-1985 . 请在IEEE 754-1985中找到更多详细信息。

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

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