如何将 2+(2/7) 转换为 IEEE 754 浮点数
有人可以向我解释将十进制格式的数字(例如 2+(2/7))转换为 IEEE 754 浮点表示的步骤吗?谢谢!
Can someone explain to me the steps to convert a number in decimal format (such as 2+(2/7)) into IEEE 754 Floating Point representation? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,
2 + 2/7
并不是大多数人所说的“十进制格式”。 “十进制格式”更常用于表示如下数字:即使
...
也有点快速和宽松。更常见的是,该数字会被截断或四舍五入到一定数量的小数位数:当然,此时,它不再完全等于
2 + 2/7
,但“足够接近”对于大多数用途。我们执行类似的操作将数字转换为 IEEE-754 格式;我们首先以 2 为基数编写数字,而不是以 10 为基数:
接下来,我们将数字“标准化”,将其写为
2^e * 1.xxx...
的形式,表示某个指数 < code>e(具体来说,我们的数字的前导位的数字位置):此时,我们必须选择特定的 IEEE-754 格式,因为我们需要知道要保留多少位数字。我们选择“单精度”,它有 24 位有效数。我们将重复的二进制数舍入为 24 位:
因为要舍入的尾随位大于
1000...
,所以该数字向上舍入为:现在,这个值实际上如何在 IEEE 中进行编码-754格式?单精度格式有一个前导符号位(零,因为数字是正数),后面是包含二进制值
127 + e
的八位,后面是有效数的小数部分:十六进制,这给出
0x40124925
。First,
2 + 2/7
isn't in what most people would call "decimal format". "Decimal format" would more commonly be used to indicate a number like:Even the
...
is a little bit fast and loose. More commonly, the number would be truncated or rounded to some number of decimal digits:Of course, at this point, it is no longer exactly equal to
2 + 2/7
, but is "close enough" for most uses.We do something similar to convert a number to a IEEE-754 format; instead of base 10, we begin by writing the number in base 2:
Next we "normalize" the number, by writing it in the form
2^e * 1.xxx...
for some exponente
(specifically, the digit position of the leading bit of our number):At this point, we have to choose a specific IEEE-754 format, because we need to know how many digits to keep around. Let's choose "single-precision", which has a 24-bit significand. We round the repeating binary number to 24 bits:
Because the trailing bits to be rounded off are larger than
1000...
, the number rounds up to:Now, how does this value actually get encoded in IEEE-754 format? The single-precision format has a leading signbit (zero, because the number is positive), followed by eight bits that contain the value
127 + e
in binary, followed by the fractional part of the significand:In hexadecimal, this gives
0x40124925
.