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

后端 未结 7 507
感情败类
感情败类 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:50

    Think of it this way

    (dot) 2^-1 2^-2 2^-3 etc

    so

    . 0/2 + 1/4 + 0/8 + 0/16 etc

    See http://floating-point-gui.de/formats/binary/

    0 讨论(0)
  • 2020-12-28 13:52

    You can convert the part after the decimal point to another base by repeatedly multiplying by the new base (in this case the new base is 2), like this:

    0.25 * 2 = 0.5
    

    -> The first binary digit is 0 (take the integral part, i.e. the part before the decimal point).

    Continue multiplying with the part after the decimal point:

    0.5 * 2 = 1.0
    

    -> The second binary digit is 1 (again, take the integral part).

    This is also where we stop because the part after the decimal point is now zero, so there is nothing more to multiply.

    Therefore the final binary representation of the fractional part is: 0.012.

    Edit:

    Might also be worth noting that it's quite often that the binary representation is infinite even when starting with a finite fractional part in base 10. Example: converting 0.210 to binary:

    0.2 * 2 = 0.4   ->   0
    0.4 * 2 = 0.8   ->   0
    0.8 * 2 = 1.6   ->   1
    0.6 * 2 = 1.2   ->   1
    0.2 * 2 = ...
    

    So we end up with: 0.001100110011...2.

    Using this method you see quite easily if the binary representation ends up being infinite.

    0 讨论(0)
  • 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 .

    0 讨论(0)
  • 2020-12-28 14:07

    2.00010 = 2+1 = 10.0002
    1.00010 = 2+0 = 01.0002
    0.50010 = 2-1 = 00.1002
    0.25010 = 2-2 = 00.0102
    0.12510 = 2-3 = 00.0012

    0 讨论(0)
  • 2020-12-28 14:10

    You can think of 0.25 as 1/4.

    Dividing by 2 in (base 2) moves the decimal point one step left, the same way dividing by 10 in (base 10) moves the decimal point one step left. Generally dividing by M in (base M) moves the decimal point one step left.

    so

    base 10                  base 2
    --------------------------------------
    1                      =>      1
    1/2 = 0.5              =>    0.1
    0.5/2 = 1/4 = 0.25     =>   0.01 
    0.25/2 = 1/8 = 0.125   =>  0.001
    .
    .
    .
    

    etc.

    0 讨论(0)
  • 2020-12-28 14:11

    Simple place value. In base 10, you have these places:

    ... 103 102 101 100 . 10-1 10-2 10-3 ...

    ... thousands, hundreds, tens, ones . tenths, hundredths, thousandths ...

    Similarly, in binary (base 2) you have:

    ... 23 22 21 20 . 2-1 2-2 2-3 ...

    ... eights, fours, twos, ones . halves, quarters, eighths ...

    So the second place after the . in binary is units of 2-2, well known to you as units of 1/4 (or alternately, 0.25).

    0 讨论(0)
提交回复
热议问题