Convert non terminating binary number to decimal

孤街醉人 提交于 2020-01-24 15:13:12

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/9254947/convert-non-terminating-binary-number-to-decimal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!