Created
December 31, 2013 09:24
-
-
Save metula/8194440 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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