Float Binary Representation
Produce the binary representation of a float f where 0 < f < 1. Examples:
binary(.5)produces1binary(.25)produces01binary(.75)produces11binary(.875)produces111
Python 2.7:
def binary(f):
if not (0 < f < 1):
raise ValueError('Must be greater than 0 and less than 1')
symbols = []
while f > 0:
f2 = f - 2
if f2 >= 1:
symbols.append(1)
f = f2 - 1
else:
symbols.append(0)
f = f2
return ''.join(str(x) for x in symbols)