开发者

Converting a repeating binary number to decimal (express as a series?)

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 serie开发者_如何学Gos I can compute at the limit as n->inf without too much success so far.


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.


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.


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜