Completes correct function for converting 2 bytes to Float16
This commit is contained in:
48
python-driver/test_utilities.py
Normal file
48
python-driver/test_utilities.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from utilities import int_to_float16
|
||||
import struct
|
||||
from math import copysign, frexp, isinf, isnan, trunc
|
||||
|
||||
NEGATIVE_INFINITY = b'\x00\xfc'
|
||||
POSITIVE_INFINITY = b'\x00\x7c'
|
||||
POSITIVE_ZERO = b'\x00\x00'
|
||||
NEGATIVE_ZERO = b'\x00\x80'
|
||||
# exp=2**5-1 and significand non-zero
|
||||
EXAMPLE_NAN = struct.pack('<H', (0b11111 << 10) | 1)
|
||||
|
||||
test_values = [
|
||||
(0b0000000000000000, 0.),
|
||||
(0b1000000000000000, -0.),
|
||||
(0b0011110000000000, 1),
|
||||
(0b0011110000000001, 1.0009765625),
|
||||
(0b1011110000000001, -1.0009765625),
|
||||
(0b1100000000000000, -2),
|
||||
(0b0100000000000000, 2),
|
||||
(0b0111101111111111, 65504.),
|
||||
(0b1111101111111111, -65504.),
|
||||
(0b0000010000000000, 6.10352e-5),
|
||||
(0b0000001111111111, 6.09756e-5), # subnormal
|
||||
(0b0000000000000001, 5.96046e-8), # subnormal
|
||||
(0b0111110000000000, float('infinity')),
|
||||
(0b1111110000000000, float('-infinity')),
|
||||
(0b0011010101010101, 0.333251953125)
|
||||
]
|
||||
|
||||
|
||||
def pad(string_to_pad, char_to_pad_with, num_chars):
|
||||
"""Pad a string with characters."""
|
||||
padded = char_to_pad_with * num_chars + string_to_pad
|
||||
return padded[-16:]
|
||||
|
||||
|
||||
def split_into_parts(inp_str):
|
||||
"""Split the binary string."""
|
||||
return inp_str[0:1] + " " + inp_str[1:6] + " " + inp_str[7:15]
|
||||
|
||||
|
||||
for t in test_values:
|
||||
expected = t[1]
|
||||
actual = int_to_float16(t[0])
|
||||
matches = expected == actual
|
||||
inp = split_into_parts(pad("{0:b}".format(t[0]), "0", 16))
|
||||
print("{}: {} == {} for int_to_float16({})".format(matches, actual, expected, inp))
|
||||
print("----")
|
||||
Reference in New Issue
Block a user