Python Programming / Floating Point Decimal to Binary Conversion
Python Program to Convert Floating-Point Decimal to Binary
Floating Point decimal conversion to binary using a python program.
Decimal vs Binary
The decimal numeral system is a system in which the numbers are expressed in scales of 10. Whereas the binary numeral system involves expressing numbers in scales of 2.
Procedure
Step 1: The whole number part(59) of 59.65625 is divided by 2 until the quotient becomes 1. The remainder(carry) that is accumulated(either 1 or 0) till the last sequence of operation that gives the quotient 1 is taken in the reverse order of creation, so as to obtain 111011.
59/2 = 29 (1 is taken as carry)
29/2 = 14 (1 is taken as carry)
14/2 = 7 (0 is taken as carry)
7/2 = 3 (1 is taken as carry)
3/2 = 1 (1 is taken as carry)
So when we reverse the carry, we get 11011. Finally, we place the final quotient at the very beginning of the 11011 to get 111011.
Step 2: The decimal part is multiplied by 2 as many times as the number of decimal places in the final result required. To elaborate, let's take the above-mentioned example of 59.65625. Here 0.65625 = 65625 * 10^-5.
0.656252 = 1.31250 (1 is taken as carry)
0.312502 = 0.62500 (0 is taken as carry)
0.625002 = 1.25000 (1 is taken as carry)
0.250002 = 0.50000 (0 is taken as carry)
0.50000*2 = 1.00000 (1 is taken as carry)
When we bring the calculations together, we obtain 111011.10101
The Python Program:
The code starts with receiving input from the user.
try:
num = float(input('Enter a floating point decimal number: '))
except(ValueError):
print('Please enter a valid floating point decimal')
The number of decimal places to which the final result is to be adjusted to is received from the end-user.
places = int(input('Enter the number of decimal places in the result: '))
The whole_list(list of the whole numbers) and dec_list(list of the decimal numbers) are declared as global variables.
global whole_list
global dec_list
The entered floating-point decimal is split into whole number and decimal.
whole, dec = str(num).split('.')
whole = int(whole)
dec = int(dec)
The whole number part is divided by 2 and the ensuing quotient is then divided by 2. The process continued until the quotient is 1. The remainder thus formed by each iteration of the while loop is added to whole_list(list of the whole numbers).
while (whole / 2 >= 1):
i = int(whole % 2)
whole_list.append(i)
whole /= 2
The decimal part is multiplied by 2 and the resultant product is split into whole number and decimal. The whole number thus formed is added to dec_list(list of the decimal numbers). The process is continued up to the number of decimal places to which the final result is to be adjusted.
while (counter <= places):
decproduct = decproduct * (10**-(len(str(decproduct))))
decproduct *= 2
decwhole, decdec = str(decproduct).split('.')
decwhole = int(decwhole)
decdec = int(decdec)
dec_list.append(decwhole)
decproduct = decdec
counter += 1
If the length of whole_list is greater than 1(for instance, 1.0567 will have a single element whole_list), the list is reversed. Subsequently, 1 is inserted as the first element of whole_list.
if(len(wholeList) > 1):
wholeList.reverse()
wholeList.insert(0, 1)
Finally, the results are printed.
Enter a floating point decimal number: 59.65625
Enter the number of decimal places in the result: 5
************************************************************
The binary number of 59.65625 is:
1 1 1 0 1 1 . 1 0 1 0 1
************************************************************
Github link:
Happy Coding!!!