Skip to content

Instantly share code, notes, and snippets.

@kms70847
Last active May 16, 2019 12:32
Show Gist options
  • Save kms70847/c1915e9f4be29fe32e1b to your computer and use it in GitHub Desktop.
Save kms70847/c1915e9f4be29fe32e1b to your computer and use it in GitHub Desktop.
Interacitve visualization of the binary form of any floating point number
from tkinter import Tk, Entry, Frame, Label, StringVar
import struct
import traceback
def float_to_int(x):
s = struct.pack("d", x)
x = 0
for value in s[::-1]:
x = (x << 8) | value
return x
def int_to_float(x):
ints = []
base = 1 << 8
while len(ints) < 8:
ints.append(x % base)
x //= base
s = bytes(ints)
return struct.unpack("d", s)[0]
def int_to_bin(x, min_digits=64):
def pad(s, x, c):
return c * (x - len(s)) + s
seq = []
while x:
seq.append(str(x%2))
x //= 2
seq.reverse()
return pad("".join(seq), min_digits, "0")
def bin_to_int(x):
return int(x, 2)
def float_to_bin(x):
return int_to_bin(float_to_int(x))
def bin_to_float(x):
return int_to_float(bin_to_int(x))
class EntryEx(Entry):
def __init__(self, parent, *args, **kargs):
cmd = (parent.register(self.on_key_change), "%P", "%s")
self.listener = None
self.var = StringVar()
self.var.set("")
Entry.__init__(self, parent, *args, textvariable=self.var, validate="key", validatecommand=cmd, **kargs)
def on_key_change(self, P, s):
if self.listener:
return self.listener(P, s)
else:
return True
def bind(self, f):
self.listener = f
def get(self):
return self.var.get()
def set(self, value):
self.var.set(value)
#global flag that prevents the text entry callbacks from calling one another endlessly.
should_reply = True
def float_changed(new, old):
global should_reply
if should_reply:
should_reply = False
try:
value = float(new)
s = float_to_bin(value)
sign.set(s[0])
exponent.set(s[1:1+11])
mantissa.set(s[-52:])
except ValueError:
sign.set("?")
exponent.set("?" * 11)
mantissa.set("?" * 52)
finally:
should_reply = True
return True
def bin_changed(new, old, field):
global should_reply
if should_reply:
should_reply = False
try:
s = ""
s += new if field == "sign" else sign.get()
s += new if field == "exponent" else exponent.get()
s += new if field == "mantissa" else mantissa.get()
value = bin_to_float(s)
float_value.set(str(value))
except Exception as e:
traceback.print_exc()
float_value.set("?")
finally:
should_reply = True
return True
root = Tk()
root.title("Float Visualizer")
left = Frame(root)
left.grid(row=0, column=0)
#spacer
Frame(root, width=50).grid(row=0, column=1)
right = Frame(root)
right.grid(row=0, column=2)
Label(left, text="Float value").pack()
float_value = EntryEx(left)
float_value.pack()
float_value.bind(float_changed)
Label(right, text="Sign").grid(row=0, column=0)
Label(right, text="Exponent").grid(row=0, column=1)
Label(right, text="Mantissa").grid(row=0, column=2)
sign = EntryEx(right, width=1)
exponent = EntryEx(right, width=11)
mantissa = EntryEx(right, width=52)
sign.grid(row=1, column=0)
exponent.grid(row=1, column=1)
mantissa.grid(row=1, column=2)
sign.bind(lambda P, s: bin_changed(P, s, "sign"))
exponent.bind(lambda P, s: bin_changed(P, s, "exponent"))
mantissa.bind(lambda P, s: bin_changed(P, s, "mantissa"))
float_value.set("1")
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment