如何重新present漂浮在二进制点。 IEEE [英] How to represent floating point in binary. IEEE

查看:157
本文介绍了如何重新present漂浮在二进制点。 IEEE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

成十进制二进制数可以类似已重新present漂浮了。现在,我读到它可以有那种彩车

0.5 0.1 0.25 <0.01 / code>, 0.125 <0.001 / code> ...等。但随后,例如,如何为0.1(以十进制)重新$ P $二进制psented?

另外,鉴于一个十进制浮点,如何将其转换为十进制等效,(给它不是那么简单的)。

编辑:所以我理解,更好的问题将是;如何将十进制浮点转换为二进制?现在我明白了,我们乘小数部分,直到它变为零。
现在是非常可能的,两个浮动点可以有相同的再presentation吧?


解决方案

由于有多少位?

0.1B:

<$p$p><$c$c>0.00011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011...

正如你可以看到它的近似值。

 二进制小数
0.1 == 1 / ^ 1 == == 1/2 0.5
0.01 == 1 / ^ 2 == == 1/4 0.25
0.11 == 1 / ^ 1 + 1/2 ^ 2 == 1/2 + 1/4 == 3/4 == 0.75

小数点重新presents 1 / ^(position_after_bit_string)后,每个位

 现在的位置:| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
         0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |

所以 0.0000001 = 1/2 ^ 7 = 0.0078125

伪code:

  decimal_value = 0
因为我在枚举位(binary_string):
    如果位== 1
         decimal_value + = 1/2 **我

有关更多信息<一个href=\"http://stackoverflow.com/questions/1089018/why-cant-decimal-numbers-be-re$p$psented-exactly-in-binary\">Why不能小数重新$ P $恰好二进制psented?

Similar to decimal binary numbers can have represent floats too. Now I read that it can have floats of the sort

0.5:0.1 , 0.25:0.01 , 0.125:0.001 ... and so on. But then, for example, how is the 0.1(in decimal) represented in binary?

Also, given a decimal float, how to convert it to the decimal equivalent, (given it is not so straightforward).

Edit: So I understand that the better question would have been ; how to convert a decimal float to binary? Now i get it that we multiply the decimal part, till it becomes zero. Now it is very much possible that two floating points can have the same representation right?

解决方案

Given how many bits?

0.1b:

0.00011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011001100110011...

As you can see it's an approximation.

Binary                                          Decimal
0.1     == 1/2^1         == 1/2              == 0.5
0.01    == 1/2^2         == 1/4              == 0.25
0.11    == 1/2^1 + 1/2^2 == 1/2 + 1/4 == 3/4 == 0.75

Each bit after the radix point represents 1/2^(position_after_bit_string).

postion:   |1|2|3|4|5|6|7|
         0.|0|0|0|0|0|0|1|

So 0.0000001 = 1/2^7 = 0.0078125

Pseudocode:

decimal_value = 0 
for i, bit in enumerate(binary_string):
    if bit == 1
         decimal_value += 1/2**i

For more info Why can't decimal numbers be represented exactly in binary?

这篇关于如何重新present漂浮在二进制点。 IEEE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆