将非终止二进制数转换为十进制数
我不知道如何将非终止二进制数(分数)转换为十进制。有人可以指导我如何做一个例子吗?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果二进制数是无终止整数,则它将是无限的(正或负)。如何用十进制表示无穷大的数?我认为是 ∞ 。
否则,如果二进制数是浮点数,那么恭喜。在许多浮点数标准(例如,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.
如果你有一个以 2 为基数的“重复小数”并且你知道它是什么
重复其中的一部分,可以将其转换为精确的
采用 p/q 表示法的有理数(其中 p 和 q 是整数)。
然后,您可以使用除法将该数字转换为普通十进制表示法,并达到您想要的精度位数。
(在某些情况下,您甚至可以写出精确的十进制值。)
第一步是将二进制数分离为其
重复部分和非重复部分。
实际上我们想要三件事:
例如,假设数字为:
其中最后一个 0011 无限重复。
我们可以将其分解如下:
二进制数的重复部分是几何级数
并可以使用此类系列的标准公式进行评估:
a + a*r + a*r^2 + a*r^3 + ... = a/(1 - r)。
将此公式应用于重复数字:
对于示例 1.00011011011...(二进制),
因此
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:
Suppose for example that the number is:
where the last 0011 is repeated indefinitely.
We can break this down as follows:
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:
For the example 1.00011011011... (binary),
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.