In binary notation, what is the meaning of the digits after the radix point “.”?

后端 未结 7 544
感情败类
感情败类 2020-12-28 13:22

I have this example on how to convert from a base 10 number to IEEE 754 float representation

Number: 45.25 (base 10) = 101101.01 (base 2) Sign: 0
Normalized          


        
7条回答
  •  情话喂你
    2020-12-28 13:55

    "Decimals" (fractional bits) in other bases are surprisingly unintuitive considering they work in exactly the same way as integers.

    base 10
    scinot 10e2  10e1  10e0 10e-1 10e-2 10e-3
    weight 100.0 10.0   1.0  0.1   0.01  0.001
    value  0     4      5     .2      5      0
    
    base 2
    scinot 2e6 2e5 2e4 2e3 2e2 2e1 2e0 2e-1 2e-2 2e-3
    weight 64  32  16   8   4   2   1   .5   .25 .125
    value   0   1   0   1   1   0   1   .0    1    0   
    

    If we start with 45.25, that's bigger/equal than 32, so we add a binary 1, and subtract 32.
    We're left with 13.25, which is smaller than 16, so we add a binary 0.
    We're left with 13.25, which is bigger/equal than 8, so we add a binary 1, and subtract 8.
    We're left with 05.25, which is bigger/equal than 4, so we add a binary 1, and subtract 4.
    We're left with 01.25, which is smaller than 2, so we add a binary 0.
    We're left with 01.25, which is bigger/equal than 1, so we add a binary 1, and subtract 1.
    With integers, we'd have zero left, so we stop. But:
    We're left with 00.25, which is smaller than 0.5, so we add a binary 0.
    We're left with 00.25, which is bigger/equal to 0.25, so we add a binary 1, and subtract 0.25.
    Now we have zero, so we stop (or not, you can keep going and calculating zeros forever if you want)

    Note that not all "easy" numbers in decimal always reach that zero stopping point. 0.1 (decimal) converted into base 2, is infinitely repeating: 0.0001100110011001100110011... However, all "easy" numbers in binary will always convert nicely into base 10.

    You can also do this same process with fractional (2.5), irrational (pi), or even imaginary(2i) bases, except the base cannot be between -1 and 1 inclusive .

提交回复
热议问题