如何将 2+(2/7) 转换为 IEEE 754 浮点数

发布于 2024-11-02 22:52:40 字数 61 浏览 0 评论 0原文

有人可以向我解释将十进制格式的数字(例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只是偏爱你 2024-11-09 22:52:40

首先,2 + 2/7 并不是大多数人所说的“十进制格式”。 “十进制格式”更常用于表示如下数字:

2.285714285714285714285714285714285714285714...

即使 ... 也有点快速和宽松。更常见的是,该数字会被截断或四舍五入到一定数量的小数位数:

2.2857142857142857

当然,此时,它不再完全等于2 + 2/7,但“足够接近”对于大多数用途。

我们执行类似的操作将数字转换为 IEEE-754 格式;我们首先以 2 为基数编写数字,而不是以 10 为基数:

10.010010010010010010010010010010010010010010010010010010010010...

接下来,我们将数字“标准化”,将其写为 2^e * 1.xxx... 的形式,表示某个指数 < code>e(具体来说,我们的数字的前导位的数字位置):

2^1 * 1.0010010010010010010010010010010010010010010010010010010010010...

此时,我们必须选择特定的 IEEE-754 格式,因为我们需要知道要保留多少位数字。我们选择“单精度”,它有 24 位有效数。我们将重复的二进制数舍入为 24 位:

2^1 * 1.00100100100100100100100  10010010010010010010010010010010010010...
           24 leading bits          bits to be rounded away

因为要舍入的尾随位大于 1000...,所以该数字向上舍入为:

2^1 * 1.00100100100100100100101

现在,这个值实际上如何在 IEEE 中进行编码-754格式?单精度格式有一个前导符号位(零,因为数字是正数),后面是包含二进制值 127 + e 的八位,后面是有效数的小数部分

0 10000000 00100100100100100100101
s exponent fraction of significand

:十六进制,这给出 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:

2.285714285714285714285714285714285714285714...

Even the ... is a little bit fast and loose. More commonly, the number would be truncated or rounded to some number of decimal digits:

2.2857142857142857

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:

10.010010010010010010010010010010010010010010010010010010010010...

Next we "normalize" the number, by writing it in the form 2^e * 1.xxx... for some exponent e (specifically, the digit position of the leading bit of our number):

2^1 * 1.0010010010010010010010010010010010010010010010010010010010010...

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:

2^1 * 1.00100100100100100100100  10010010010010010010010010010010010010...
           24 leading bits          bits to be rounded away

Because the trailing bits to be rounded off are larger than 1000..., the number rounds up to:

2^1 * 1.00100100100100100100101

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:

0 10000000 00100100100100100100101
s exponent fraction of significand

In hexadecimal, this gives 0x40124925.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文