A converting between the decimal to single-precision floating,but i intro to this,so did any useful references or any summarize can help?
Thousand Thanks!
so,the python function will be like
require a user to select convert from decimal to to floating point number or floating point number convert to decimal.
input value
will show the step such as
i. Sign
ii. Exponent
iii. Mantissa
return the result.
below is the example i searched from internet but this is 64bit so,how change it to 32bit and show the step when converting?
import struct
getBin = lambda x: x > 0 and str(bin(x))[2:] or "-" + str(bin(x))[3:]
def floatToBinary64(value):
val = struct.unpack('Q', struct.pack('d', value))[0]
return getBin(val)
def binaryToFloat(value):
hx = hex(int(value, 2))
return struct.unpack("d", struct.pack("q", int(hx, 16)))[0]
# floats are represented by IEEE 754 floating-point format which are
# 64 bits long (not 32 bits)
# float to binary
binstr = floatToBinary64(19.5)
print('Binary equivalent of 19.5:')
print(binstr + '\n')
# binary to float
fl = binaryToFloat(binstr)
print('Decimal equivalent of ' + binstr)
print(fl)