0

I'm trying to write a basic FP16 based calculator in python to help me debug some hardware. Can't seem to find how to convert 16b hex values unto floating point values I can use in my code to do the math. I see lots of online references to numpy but I think the float16 constructor expects a string like float16("1.2345"). I guess what I'm looking for is something like float16("0xabcd").

Thanks!

ajcrm125
  • 323
  • 2
  • 12

1 Answers1

1

The numpy.float16 is indeed a signed floating point format with a 5-bit exponent and 10-bit mantissa.

To get the result of your example:

import numpy as np

np.frombuffer(b'\xab\xcd', dtype=np.float16, count=1)

Result:

array([-22.67], dtype=float16)

Or, to show how you can encode and decode the other example 1.2345:

import numpy as np

a = np.array([1.2345], numpy.float16)
b = a.tobytes()
print(b)
c = np.frombuffer(b, dtype=np.float16, count=1)
print(c)

Result:

b'\xf0<'
[1.234]

If you literally needed to turn the string you provided into an FP16:

import numpy as np

s = "0xabcd"
b = int("0xabcd", base=16).to_bytes(2, 'big')
print(b)
c = np.frombuffer(b, dtype=np.float16, count=1)
print(c)

Output:

b'\xab\xcd'
[-22.67]
Grismar
  • 27,561
  • 4
  • 31
  • 54
  • But that's not what I need. I want the input to be a hex string. Not a float string. – ajcrm125 Aug 25 '22 at 02:19
  • Clearly, `b'\xab\xcd'` is a hex string? What you provided (`"0xabcd"`) is a string containing an hexadecimal integer representation, if that's what you wanted to use? – Grismar Aug 25 '22 at 02:26
  • Sorry I missed that. Thx! – ajcrm125 Aug 25 '22 at 14:30
  • That's alright, I added an extra example taking exactly the string you suggested, but I assumed you actually just had two bytes in a `bytes` variable, which is what the previous examples assumed (having `bytes` in `b`) - if the answer now answers your question, please use the checkmark to indicate your question has been answered. Otherwise, feel free to ask for clarification. – Grismar Aug 25 '22 at 20:52