N=343# #decimal integer
N2=bin(N)[2:]# #binary equivalent of N
print 'Binary equivalent of 343 is',N2
from math import floor
N=0.392# #decimal
def float_to_binary(num):
exponent=0
shifted_num=num
while shifted_num != int(shifted_num):
shifted_num*=2
exponent+=1
if exponent==0:
return '{0:0b}'.format(int(shifted_num))
binary='{0:0{1}b}'.format(int(shifted_num),exponent+1)
integer_part=binary[:-exponent]
fractional_part=binary[-exponent:].rstrip('0')
return '{0}.{1}'.format(integer_part,fractional_part)
DP =float_to_binary(N)[0:9]
print 'Binary form of 0.392 is',DP
n=343.392#
n1=n%1 # decimal float part
n2=int(n-n1) # decimal integer part
b1=bin(n2)[2:] # binary integer part
def float_to_binary(num):
exponent=0
shifted_num=num
while shifted_num != int(shifted_num):
shifted_num*=2
exponent+=1
if exponent==0:
return '{0:0b}'.format(int(shifted_num))
binary='{0:0{1}b}'.format(int(shifted_num),exponent+1)
integer_part=binary[:-exponent]
fractional_part=binary[-exponent:].rstrip('0')
return '{0}.{1}'.format(integer_part,fractional_part)
b2 =float_to_binary(n2)[0:9] # binary float part
#combining these two
b=str(b1)+'.'+str(b2)
print 'binary form of 343.392 : ',b