将重复的二进制数转换为十进制(表示为系列?)

发布于 2024-08-18 22:50:34 字数 624 浏览 9 评论 0原文

给定一个重复的二进制数,例如 0.(0011) 或 0.0(101),如何将其转换为十进制?

到目前为止,我已经能够挖掘出将终止二进制数转换为十进制数的简单方法,如下所示:

res(N+2) = res(N+1) / 2 + res(N)

其中 res 是步骤 N 之后的结果,N 是当前迭代(N=0; n-> ;(二进制数字))。将其重复应用于非终止二进制数可以得到很好的近似值,例如

dec:0.4 || bin: 0.(0110):

0     / 2 + 0 = 0
0     / 2 + 0 = 0
0     / 2 + 1 = 1
1/2   / 2 + 1 = 3/2
3/2   / 2 + 0 = 3/4
3/4   / 2 + 0 = 3/8
3/8   / 2 + 1 = 19/16
19/16 / 2 + 1 = 51/32
51/32 / 2 + 0 = 51/64
51/64 / 2 + 0 = 51/128 = 0.3984

大约为 0.4。

所以,我有一种计算近似值的方法,但我正在努力寻找一种表达方法。我已经开始尝试将其写成一个系列,我可以在 n->inf 的极限下进行计算,但到目前为止还没有取得太大的成功。

Given a binary number that repeats, for example 0.(0011) or 0.0(101), how would one go about converting it to decimal?

What I've been able to dig up so far is the simple method for converting a terminating binary number to decimal, as below:

res(N+2) = res(N+1) / 2 + res(N)

where res is the result after step N, and N is the current iteration (N=0; n->(num binary digits)). Applying that repeatedly to a nonterminating binary number gives a good approximation, for example

dec:0.4 || bin: 0.(0110):

0     / 2 + 0 = 0
0     / 2 + 0 = 0
0     / 2 + 1 = 1
1/2   / 2 + 1 = 3/2
3/2   / 2 + 0 = 3/4
3/4   / 2 + 0 = 3/8
3/8   / 2 + 1 = 19/16
19/16 / 2 + 1 = 51/32
51/32 / 2 + 0 = 51/64
51/64 / 2 + 0 = 51/128 = 0.3984

which is approximately 0.4.

So, I've got a means of calculating approximates, but I'm struggling with finding a way to express this. I've started trying to write it as a series I can compute at the limit as n->inf without too much success so far.

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

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

发布评论

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

评论(4

画离情绘悲伤 2024-08-25 22:50:34

给定一个重复的二进制数,例如 0.(0011) 或 0.0(101),如何将其转换为十进制?

这可以用与十进制相同的方式用二进制来解决(即可以确定精确的有理数)。在十进制中,如果我们有 0.(567),并且我们想要确定它所代表的确切有理数,我们只需将 567 作为我们的分子,并且999(具有 n 9 的数字,其中 n 是重复组中的位数)作为我们的分母:

0.(567) = 567/999 = 189/333 = 63/111

现在是最低的。此过程是完整无限几何级数结果的蒸馏 @Rick Regan 提及

在二进制中,我们做同样的事情,只是我们需要 n 1n 9s 作为分母>s(因为 1 是二进制中的最高位)。例如,

0.(0011) = 0011 / 1111 =(in decimal) 3/15 = 1/5

如果重复组之前有数字,只需围绕此计算进行一些算术:例如,0.0(101) 只是 0.(101) 除以2. 后者是 101 / 1115/7,因此 0.0(101)5/14 >。

Given a binary number that repeats, for example 0.(0011) or 0.0(101), how would one go about converting it to decimal?

This can be solved (ie the exact rational quantity can be determined) in binary just the same way as in decimal. In decimal, if we have, say , 0.(567), and we want to determine the exact rational quantity it represents, we simply take 567 as our numerator, and 999 (the number that has n 9s, where n is the number of digits in the repeating group) as our denominator:

0.(567) = 567/999 = 189/333 = 63/111

which is now in lowest terms. This process is a distillation of the full infinite geometric series result mentioned by @Rick Regan.

In binary we do the same thing, except that instead of n 9s as our denominator, we want n 1s (as 1 is the highest digit in binary). So for example

0.(0011) = 0011 / 1111 =(in decimal) 3/15 = 1/5

Where you have digits before the repeating group, just do some arithmetic around this calculation: for example, 0.0(101) is just 0.(101) divided by 2. This latter is 101 / 111, or 5/7, so 0.0(101) is 5/14.

七秒鱼° 2024-08-25 22:50:34

获得准确答案的一种方法是使用无限几何级数。分数 r 的无穷幂和,对于指数 1 到无穷大,0 <= r < 1,是r/(1-r)。

在您的示例中,0.(0011), 0.0011 表示分数 3/16。因式分解 3,得到 r=1/16。 r/(1-r) = (1/16)/(15/16) = 1/15。将其乘以您分解出的 3,即可得到答案:3/15 = 1/5 = 0.2。

One way to get an exact answer is using infinite geometric series. The infinite sum of powers of a fraction r, for exponents 1 to infinity, 0 <= r < 1, is r/(1-r).

In your example, 0.(0011), 0.0011 represents the fraction 3/16. Factor out the 3 and you get r=1/16. r/(1-r) = (1/16)/(15/16) = 1/15. Multiply that by the 3 you factored out and you get your answer: 3/15 = 1/5 = 0.2.

虚拟世界 2024-08-25 22:50:34

即使计算机也不能完全正确地理解它。通常,该值只是四舍五入。如果您开始以过高的精度显示浮点值,您最终会得到奇怪的值,例如 0.3984 而不是 0.4。

将任何基数的任何小数转换为另一个基数通常会导致精度损失。你无法神奇地恢复它。这是您永远不应该在计算金钱等重要内容的程序中使用浮点数或双精度数的主要原因。

继续下去,直到你认为自己已经足够精确,然后四舍五入。

Even computers don't get it quite right. Usually, the value is simply rounded. If you start displaying float values with too much precision, you'll end up with weird values like 0.3984 instead of 0.4.

Converting any decimal of any base to another base often induces a loss of precision. You can't recover that magically. It's the main reason you should never use floats or doubles in a program that counts important stuff like money.

Just keep going until you consider you've ben precise enough, and round it off.

蓝梦月影 2024-08-25 22:50:34

如果您使用最大的数字(以 10 为基数的 9,以 2 为基数的 1)执行与十进制相同的操作,则可以一步将所有内容放在一起,其次数等于重复数字的次数,并且 0 等于数字的数量重复数字之前。希望这个例子能清楚地说明这一点:

0.196(2) = (196*9 + 2)/(9000)
0.12(034) = (12*999 + 34)/99900

b0.01(011) = (b1*b111 + b11)/b11100 = (1*7 + 3)/(7*4) = 10/28

You can put it all together in one step if you do the same thing as in decimal using the biggest digit (9 in base 10, 1 in base 2) the number of times equal to the digits repeated and 0s equal to the number of digits before the repeated digits. Hopefully the example makes this clear:

0.196(2) = (196*9 + 2)/(9000)
0.12(034) = (12*999 + 34)/99900

b0.01(011) = (b1*b111 + b11)/b11100 = (1*7 + 3)/(7*4) = 10/28
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文