# Picking apart a floating point number¶

In [33]:
# Never mind the details of this function...

def pretty_print_fp(x):
print("---------------------------------------------")
print("Floating point structure for %r" % x)
print("---------------------------------------------")
import struct
s = struct.pack("d", x)

def get_bit(i):
byte_nr, bit_nr = divmod(i, 8)
return int(bool(
s[byte_nr] & (1 << bit_nr)
))

def get_bits(lsb, count):
return sum(get_bit(i+lsb)*2**i for i in range(count))

# https://en.wikipedia.org/wiki/Double_precision_floating-point_format

print("Sign bit (1:negative):", get_bit(63))

stored_exponent = get_bits(52, 11)
fraction = get_bits(0, 52)

if stored_exponent == 0:
exponent = -1022
significand = fraction
else:
exponent = stored_exponent - 1023
significand = fraction + 2**52

print("Stored exponent value: %d" % stored_exponent)
print("Mathematical exponent: %d" % exponent)

print("Fraction (bin): {0:052b}".format(fraction))
print("Signif. (bin): {0:053b}".format(significand))
print("Significand (decimal, scaled to <=2):", repr(significand / (2**52)))

In [41]:
pretty_print_fp(1)

---------------------------------------------
Floating point structure for 1
---------------------------------------------
Sign bit (1:negative): 0
Stored exponent value: 1023
Mathematical exponent: 0
Fraction (bin): 0000000000000000000000000000000000000000000000000000
Signif. (bin): 10000000000000000000000000000000000000000000000000000
Significand (decimal, scaled to <=2): 1.0


Things to try:

• Twiddle the sign bit
• 1,2,4,8
• 0.5,0.25
• $2^{\pm 1023}$, $2^{\pm 1024}$
• float("nan")
In [ ]: