Updates with fixed int_to_float16 utility function

This commit is contained in:
Patrick McDonagh
2018-01-09 09:48:21 -06:00
parent 8e5cd8fd83
commit 5e8bf59a92

View File

@@ -15,12 +15,25 @@ def get_public_ip_address():
def int_to_float16(int_to_convert): def int_to_float16(int_to_convert):
"""Convert integer into float16 representation.""" """Convert integer into float16 representation."""
bin_rep = ('0' * 16 + '{0:b}'.format(int_to_convert))[-16:] bin_rep = ('0' * 16 + '{0:b}'.format(int_to_convert))[-16:]
sign = 1.0
if int(bin_rep[0]) == 1:
sign = -1.0
exponent = float(int(bin_rep[1:6], 2))
if exponent == 30:
fraction = float(int("1" + bin_rep[7:17], 2))
else:
fraction = float(int(bin_rep[7:17], 2))
sign = -1 ** int(bin_rep[0]) if exponent == float(0b00000):
exponent = int(bin_rep[1:6], 2) return sign * 2 ** -14 * fraction / (2.0 ** 10.0)
fraction = int(bin_rep[7:17], 2) elif exponent == float(0b11111):
if fraction == 0:
return sign * 2 ** (exponent - 15) * float("1.{}".format(fraction)) return sign * float("inf")
else:
return float("NaN")
else:
frac_part = 1.0 + fraction / (2.0 ** 10.0)
return sign * (2 ** (exponent - 15)) * frac_part
def ints_to_float(int1, int2): def ints_to_float(int1, int2):