简体   繁体   English

将 integer 转换为 IEEE 浮点数?

[英]Convert integer to IEEE floating point?

I am currently reading "Computer Systems: A Programmer's Perspective".我目前正在阅读“计算机系统:程序员的视角”。 In the book, big-endian is used(most significant bits first).在本书中,使用了大端(最高有效位在前)。 In the context of IEEE floating point numbers, using 32-bit single-precision, here is a citation of conversion between an integer and IEEE floating point:在 IEEE 浮点数的上下文中,使用 32 位单精度,这里引用了 integer 和 IEEE 浮点之间的转换:

One useful exercise for understanding floating-point representations is to convert sample integer values into floating-point form.理解浮点表示的一项有用练习是将示例 integer 值转换为浮点形式。 For example, we saw in Figure 2.15 that 12,345 has binary representation [11000000111001].例如,我们在图 2.15 中看到 12,345 具有二进制表示 [11000000111001]。 We create a normalized representation of this by shifting 13 positions to the right of a binary point, giving 12,345 = 1.10000001110012 × 2^13.我们通过将 13 个位置移到二进制点的右侧来创建它的标准化表示,得到 12,345 = 1.10000001110012 × 2^13。 To encode this in IEEE single-precision format, we construct the fraction field by dropping the leading 1 and adding 10 zeros to the end, giving binary representation [10000001110010000000000].为了以 IEEE 单精度格式对其进行编码,我们通过删除前导 1 并在末尾添加 10 个零来构造分数字段,从而给出二进制表示 [10000001110010000000000]。 To construct the exponent field, we add bias 127 to 13, giving 140, which has binary representation [10001100].为了构造指数场,我们将偏差 127 添加到 13,得到 140,它具有二进制表示 [10001100]。 We combine this with a sign bit of 0 to get the floating-point representation in binary of [01000110010000001110010000000000].我们将其与符号位 0 相结合,以获得二进制的浮点表示 [01000110010000001110010000000000]。

What I do not understand is "by dropping the leading 1 and adding 10 zeros to the end, giving binary representation [10000001110010000000000]."我不明白的是“通过删除前导 1 并在末尾添加 10 个零,给出二进制表示 [10000001110010000000000]。” If big-endian is used, why can you add 10 zeros to the end of 1000000111001?如果使用大端序,为什么可以在 1000000111001 的末尾添加 10 个零? Doesn't that lead to another value than that after the binary point?这不会导致二进制点之后的另一个值吗? It would make sense to me if we added 10 zeros in the front since the final decimal value would still be that originally after the binary point.如果我们在前面添加 10 个零对我来说很有意义,因为最终的十进制值仍然是二进制点之后的值。

Why/how can you add 10 zeros to the back without changing the value if big-endian is used?如果使用大端序,为什么/如何在不改变值的情况下在后面添加 10 个零?

This is how the number 12345 is represented as a 32-bit single-precision IEEE754 float:这是数字12345表示为 32 位单精度 IEEE754 浮点数的方式:

                  3  2          1         0
                  1 09876543 21098765432109876543210
                  S ---E8--- ----------F23----------
          Binary: 0 10001100 10000001110010000000000
             Hex: 4640 E400
       Precision: SP
            Sign: Positive
        Exponent: 13 (Stored: 140, Bias: 127)
       Hex-float: +0x1.81c8p13
           Value: +12345.0 (NORMAL)

Since this is a NORMAL value, the fractional part is interpreted with an implicit 1-bit;因为这是一个NORMAL值,所以小数部分用隐含的 1 位解释; that is it's 1.10000001110010000000000 .那就是1.10000001110010000000000 So, to fill the 23 bit mantissa you simply add 10 0's at the end as it doesn't change the value.因此,要填充 23 位尾数,您只需在末尾添加 10 个 0,因为它不会改变值。

Endianness isn't really related to how these numbers are represented, as each bit has a fixed meaning.字节序实际上与这些数字的表示方式无关,因为每个位都有固定的含义。 But in general, the most-significant-bit is to the left in both the exponent and the mantissa.但一般来说,最高有效位在指数和尾数的左侧。

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

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