Can anyone help me explain this code which converts decimals to binary?

Can someone help me explain this code which converts decimals to binary?

Convert decimals to binary:

x = float(raw_input('Enter a decimal number between 0 and 1: '))

p = 0
while ((2**p)*x)%1 != 0:
    print('Remainder = ' + str((2**p)*x - int((2**p)*x)))
    p += 1

num = int(x*(2**p))

result = ''
if num == 0:
    result = '0'
while num > 0:
    result = str(num%2) + result
    num = num/2

for i in range(p - len(result)):
    result = '0' + result

result = result[0:-p] + '.' + result[-p:]
print('The binary representation of the decimal ' + str(x) + ' is ' + str(result))

      

+3


source to share


1 answer


I don't think this code is written that well, but here's a rough idea. First while loop:

while ((2**p)*x)%1 != 0: ...

      

calculates how many places in binary to the right of the decimal point will be the result. To use a familiar analogy, let's work with base 10. If the number is 1.234 and I want to convert to base 10 (no problem), then the answer will be to the right of the decimal point. But how do I know this? Well, I multiply 10, 100, 1000, and so on, until I have a number that doesn't have a fractional part. (...)%1

gives the fractional part (...)

and is equal to zero when ...

is an integer.

Now that I know the smallest cardinality, 2, which converts the input to an integer, we multiply it by to get an integer. This value is called num in the program. Similar to how you do it in decimal format, when you do math operations, you can ignore the decimal point and insert it after the calculation. Technically it does those steps.

  • multiplication by 10,
  • work on the number
  • divide this power by 10 after you are done.

And the same is true for power 2. Mathematically:

  conversion(x) == conversion(x*2**p)/2**p

      

From here, using num, we compute the bits in the output from least significant to most significant. This is the part that starts:



  while num > 0:

      

If the number is odd, then it is clear that the last digit of the number converted to binary will also be odd. Otherwise, the last digit will be 0. After figuring out the least significant bit, we move on to the next most significant bit, and so on.

Here's an example. Suppose the number is 5. This is odd, so now we know that the last digit in binary will be 1. Now consider the remainder of 4 (since 4 = 5-1) shift that right one place to account for its smallest - significant bit. Don't worry about moving to the right one place, because in the answer we will move one place before we insert it into the final result.

4 shifted one place - 2. It's even so that "0". To the left of the "1" we had before, we will now place this "0". 2 remains (2 = 2 - 0), so we shift 2 one space to the right, consider the remainder again, which is now 1. And that's odd, so add another "1" to the left of "01". What remains now is now 0 (1-1 = 0) So we stop. And we all got "101".

Previously, we calculated how many places to the right of the decimal point, p. So the part that starts:

 for i in range(p - len(result)):
    result = '0' + result

      

adds 0 to the beginning of a number greater than p. And after the decimal point, the right-most p-characters are added:

  result = result[0:-p] + '.' + result[-p:]

      

0


source







All Articles