将非终止二进制数转换为十进制数

发布于 2025-01-05 17:27:29 字数 48 浏览 0 评论 0原文

我不知道如何将非终止二进制数(分数)转换为十进制。有人可以指导我如何做一个例子吗?

I don't know how to convert a non terminating binary number(fraction) to decimal . Can anybody guide me how to do with an example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

与风相奔跑 2025-01-12 17:27:29

如果二进制数是无终止整数,则它将是无限的(正或负)。如何用十进制表示无穷大的数?我认为是

否则,如果二进制数是浮点数,那么恭喜。在许多浮点数标准(例如,IEEE 754)中,尾数由二进制模式表示,其中最高位的值为 1/2,第二位的值为 1/4 等。您可以通过从左边开始一位一位累加将其转换为十进制。

例如,您有一个未终止的二进制模式,例如

10111011101110111011.......

转换为十进制只需将它们累加为

1*1/2 + 0*1/ 4 + 1*1/8 + 1*1/16 + 1*1/32 + 0*1/64 + 1*1/128 + 1*1/256 ........

直到你得到足够的精确。

if the binary number is a unterminated integer, it would be infinite (positive or negative). How can you represent infinite number in decimal? I think it's .

else if the binary number is a float-point number, congratulations. In many standard of float-point number(e.g., IEEE 754), the mantissa is represented by a binary pattern in which the highest bit has a value of 1/2, the second bit has a value of 1/4 etc. So that you can convert it into decimal by accumulate each bit one by one from left.

For example, you have a unterminated binary patter say

10111011101110111011.......

to convert into decimal just accumulate them as

1*1/2 + 0*1/4 + 1*1/8 + 1*1/16 + 1*1/32 + 0*1/64 + 1*1/128 + 1*1/256 ........

until you get enough precision.

萌能量女王 2025-01-12 17:27:29

如果你有一个以 2 为基数的“重复小数”并且你知道它是什么
重复其中的一部分,可以将其转换为精确的
采用 p/q 表示法的有理数(其中 pq 是整数)。

然后,您可以使用除法将该数字转换为普通十进制表示法,并达到您想要的精度位数。
(在某些情况下,您甚至可以写出精确的十进制值。)

第一步是将二进制数分离为其
重复部分和非重复部分。

实际上我们想要三件事:

  • 非重复部分、
  • 重复数字块的第一次出现以及
  • 重复数字块的长度。

例如,假设数字为:

1.0001100110011... (binary)

其中最后一个 0011 无限重复。

我们可以将其分解如下:

  • 非重复部分是1.0(二进制),
  • 重复块的第一次出现是0.00011(二进制),
  • 重复块的长度(0011)是四个二进制数字。

二进制数的重复部分是几何级数
并可以使用此类系列的标准公式进行评估:

a + a*r + a*r^2 + a*r^3 + ... = a/(1 - r)。

将此公式应用于重复数字:

  • a 的值就是重复块第一次出现的值
  • 如果重复部分有 n 个二进制数字,则比率公式中的r为1/2^n且1 - r = (2^n - 1) /(2^n).

对于示例 1.00011011011...(二进制),

  • 从重复部分我们有 a = 0.00011(二进制)= 3/32
  • n = 4,因此 1 - < em>r = (2^4 - 1)/(2^4) = 15/16。

因此

a/(1 - r) = (3/32) / (15/16) = 3/30 = 1/10,

我们可以写成0.1 (十进制)。

当然,非重复部分是1(十进制),所以

1.00011011011...(二进制)= 1 + 0.1(十进制)= 1.1(十进制)。

在此示例中,十进制表示形式是终止且精确的。

有许多重复的二进制分数,其中没有精确的终止十进制表示,例如

0.01010101...(二进制)= 1/3 = 0.3333...(十进制)。

在这种情况下,您必须决定在一定数量的十进制数字后进行四舍五入,或者找到并描述十进制数字的重复模式。

If you have a "repeating decimal" in base 2 and you know what the
repeating part of it is, it is possible to convert it to an exact
rational number in p/q notation (where p and q are integers).

You can then use division to convert that number to ordinary decimal notation to as many digits of precision as you want.
(In some cases you can even write the exact decimal value.)

The first step is to separate the binary number into its
repeating and non-repeating parts.

Actually we want three things:

  • the non-repeating part,
  • the first occurrence of the repeating block of digits, and
  • the length of the repeating block of digits.

Suppose for example that the number is:

1.0001100110011... (binary)

where the last 0011 is repeated indefinitely.

We can break this down as follows:

  • the non-repeating part is 1.0 (binary)
  • the first occurrence of the repeating block is 0.00011 (binary), and
  • the length of the repeating block (0011) is four binary digits.

The repeating part of the binary number is a geometric series
and can be evaluated using the standard formula for such a series:

a + a*r + a*r^2 + a*r^3 + ... = a/(1 - r).

To apply this formula to the repeating digits:

  • the value of a is simply the value of the first occurrence of the repeating block
  • If the repeating part has n binary digits, the ratio r in the formula is 1/2^n and 1 - r = (2^n - 1)/(2^n).

For the example 1.00011011011... (binary),

  • from the repeating part we have a = 0.00011 (binary) = 3/32
  • and n = 4, so 1 - r = (2^4 - 1)/(2^4) = 15/16.

Therefore

a/(1 - r) = (3/32) / (15/16) = 3/30 = 1/10,

which we can write as 0.1 (decimal).

The non-repeating part, of course, is 1 (decimal), so

1.00011011011... (binary) = 1 + 0.1 (decimal) = 1.1 (decimal).

In this example the decimal representation is terminating and exact.

There are many repeating binary fractions for which where the there is no exact, terminating decimal representation, for example,

0.01010101... (binary) = 1/3 = 0.3333... (decimal).

In such cases you must either decide to round off after some number of decimal digits or find and describe the repeating pattern of decimal digits.

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