How do you convert a fraction to binary?

后端 未结 5 1589
野性不改
野性不改 2020-11-30 17:42

1/10(decimal) = 0.0001100110011... (binary)

How do I do that? Am I supposed to convert to binary and then divide? Could someone show me?

5条回答
  •  攒了一身酷
    2020-11-30 18:06

    Here is how to think of the method.

    Each time you multiply by 2, you are shifting the binary representation of the number left 1 place. You have shifted the highest digit after the point to the 1s place, so take off that digit, and it is the first (highest, therefore leftmost) digit of your fraction. Do that again, and you have the next digit.

    Converting the base of a whole number by dividing and taking the remainder as the next digit is shifting the number to the right. That is why you get the digits in the opposite order, lowest first.

    This obviously generalizes to any base, not just 2, as pointed out by GoofyBall.

    Another thing to think about: if you are rounding to N digits, stop at N+1 digits. If digit # N+1 is a one, you need to round up (since digits in binary can only be a 0 or 1, truncating with the next digit a 1 is as inaccurate as truncating a 5 in decimal).

提交回复
热议问题