Dec2Bin
#!/usr/bin/python
print "Convert decimal floating-point numbers to their binary represenation."
print "Press <Enter> without entering a number to quit."
while True:
print "decimal:",
decimal = raw_input()
try:
float(decimal)
except ValueError:
break
if decimal.find('.')==-1:
integer,fraction=decimal,''
else:
integer,fraction = decimal.split('.')
binary=''
if integer is not '':
integer = int(integer)
while integer:
binary = str(integer&1) + binary
integer >>= 1
if fraction is not '':
binary+='.'
fraction = float('.'+fraction)
while fraction:
fraction*=2
if fraction>=1:
binary+='1'
fraction-=1
else:
binary+='0'
print " binary:", binary
print