How to represent floating point in binary. IEEE

眉间皱痕 提交于 2019-12-07 23:41:33
Chris Seymour

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?

Another observation that could be helpful. The 'integer part' of a floating point number is present in the binary in its 'normal' form, for instance if the value is 25.7482 you will have the bits '11001' (25) in the floating point, with the bits following representing the fraction (actually the first '1' is never stored, it's implied in the format). If you subtract 25.0 from that number, and multiply by 10, you get 7.482, and by examining the integer part of that value, you can obtain the first fractional digit, '7'.

Subtract 7.0, multiply by 10 gives 4.82 , thus the next digit '4', and so on. This process will in theory end eventually with a zero result, since all values that can be exactly represented in floating-point format have an exact decimal representation; however, that 'exact' result could have far more digits than are actually reasonable given the precision of the original floating point (and you may need extra precision internally to obtain that fully exact result, anyhow - you need to ensure the multiplication by 10 does not generate a rounding error).

And, for numbers like 6.432e-200 ,this method is workable but not very efficient (you'd be generating 199 zeros before the first '6' emerged).

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