Skip to content

Instantly share code, notes, and snippets.

@metula
Created December 31, 2013 09:24
Show Gist options
  • Select an option

  • Save metula/8194440 to your computer and use it in GitHub Desktop.

Select an option

Save metula/8194440 to your computer and use it in GitHub Desktop.
import struct
def display_float(x):
""" prints 64 bits in the representation of the float x """
if isinstance(x, float):
q, = struct.unpack('Q', struct.pack("d", x))
full_bin = "{:064b}".format(q)
sign= full_bin[0]
exponent_plus = full_bin[1:12]
fraction=full_bin[12:]
return sign + " " + exponent_plus + " " + fraction
else:
return None
def show_fractions_between_one_and_two():
for i in range(1, 54):
x = sum([ 1/(2.**j) for j in range(i) ])
print(display_float(x), "-", x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment