Python Program to Convert any Positive Real Number to Binary string
Last Updated :
10 May, 2020
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.
Python3 1==
# 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
Similar Reads
Python program to convert Base 4 system to binary number Given a base 4 number N, the task is to write a python program to print its binary equivalent. Conversion Table: Examples: Input : N=11002233 Output : 101000010101111 Explanation : From that conversion table we changed 1 to 01, 2 to 10 ,3 to 11 ,0 to 00.Input : N=321321 Output: 111001111001Method 1:
3 min read
Python Program to Subtract Two Binary Numbers We are given two binary numbers, num1 and num2 and we have to subtract these two binary numbers and return the result. In this article, we will see how we can subtract two binary numbers in Python.Examples:Input: a = "1110" , b = "0110"Output: "1000"Explanation: We can see that when "1110" is subtra
3 min read
Python program to convert ASCII to Binary We are having a string s="Hello" we need to convert the string to its ASCII then convert the ASCII to Binary representation so that the output becomes : 01001000 01100101 01101100 01101100 0110111. To convert an ASCII string to binary we use ord() to get the ASCII value of each character and format(
2 min read
Python program to convert binary to ASCII In this article, we are going to see the conversion of Binary to ASCII in the Python programming language. There are multiple approaches by which this conversion can be performed that are illustrated below: Method 1: By using binascii module Binascii helps convert between binary and various ASCII-en
3 min read
Python program to print the binary value of the numbers from 1 to N Given a positive number N, the task here is to print the binary value of numbers from 1 to N. For this purpose various approaches can be used. The binary representation of a number is its equivalent value using 1 and 0 only. Example for k = 15, binary value is 1 1 1 1 WORKING FOR METHOD 1Method 1: U
2 min read