IEEE-754 Floating Point Converter
// 64 bit (double)
# 64 bit floats are represented by IEEE 754 floating-point format (not 32 bits)
## 1. float to binary 64 bit
## 2. binary 64 to float
import struct
# float64 to binary64
def floatToBinary64(value):
val = struct.unpack('Q', struct.pack('d', value))[0]
getBin = lambda x: x > 0 and str(bin(x))[2:] or "-" + str(bin(x))[3:]
return getBin(val)
# binary64 to float64
def binary64ToFloat(value):
hx = hex(int(value, 2))
return struct.unpack("d", struct.pack("q", int(hx, 16)))[0]
## begin
float_val = 19.5
bin64str = floatToBinary64(float_val)
print('Binary 64 equivalent of '+ str(float_val) + ':\n' + bin64str + '\n')
# binary64 to float64
float64_val = binary64ToFloat(bin64str)
print('Decimal float(IEEE745 floating-point 64) equivalent of ' + bin64str + ':\n' + str(float64_val))Last updated
Was this helpful?