Open In App

Python Program to Convert any Positive Real Number to Binary string

Given any Real Number greater than or equal to zero that is passed in as float, print binary representation of entered real number.

Examples:



Input: 123.5
Output: 1 1 1 1 0 1 1 . 1

Input: 0.25
Output: .01

Mathematical Logic along with steps done in programming:

Any real number is divided into two parts: The integer part and the Fraction part. For both parts operations and logic are different to convert them to binary representation.



Now at last combining, all the binary conversion in given below format will be the binary representation of entered real number. Firstly write the reversed sequence of remainders and a dot(point) then write integer part sequence which we obtained by multiplying fraction part by 2 and extracting integer part only.

def binarycode(m):

    a = intpartbinary(m)
    b = decimalpartbinary(m)
    c = []

    for i in range(0, len(a)):
        c.append(a[i])

    c.append('.')

    for j in range(0, len(b)):
        c.append(b[j])

    print('Binary code of given function is\n')

    for k in range(0, len(c)):
        print(c[k], end=' ')

For this, we will define a function named binarycode(). Define an empty list c=[], firstly store reversed list of remainder obtained by dividing integer part by 2 each time and then store a decimal in that list c, now store the list of integer part obtained by multiplying fraction part by 2 each time and storing only integer part. Now print list c. Finally, we will have our Binary Representation of Real Number into Binary Representation.

Below is the implementation.




# defining a function to convert 
# integer part to binary
def intpartbinary(m):
      
    a = []
    n = int(m)
      
    while n != 0:
          
        a.append(n % 2)
        n = n//2
          
    a.reverse()
    return a
  
# defining a function to convert 
# fractional part to binary
def decimalpartbinary(m):
      
    a = []
    n = m-int(m)
      
    while n != 0:
          
        x = 2 * n
        y = int(x)
        a.append(y)
        n = x-y
          
    return a
# arranging all data into suitable format
def binarycode(m):
      
    # arranging all data to our desired 
    # format
    a = intpartbinary(m)
    b = decimalpartbinary(m)
    c =[]
      
    for i in range(0, len(a)):
        c.append(a[i])
          
    c.append('.')
      
    for j in range(0, len(b)):
        c.append(b[j])
          
    print('Binary code of given function is\n')
      
    for k in range(0, len(c)):
        print(c[k], end =' ')
          
  
# Driver Code
binarycode(123.5)

Output:

Binary code of given function is

1 1 1 1 0 1 1 . 1 

Article Tags :