5

I was only able to convert a decimal into a binary single-precision IEEE754, using the struct.pack module, or do the opposite (float16 or float32) using numpy.frombuffer

Is it possible to convert a decimal to a binary half precision floating point, using Numpy?

I need to print the result of the conversion, so if I type "117.0", it should print "0101011101010000"

jfs
  • 399,953
  • 195
  • 994
  • 1,670
Kaio
  • 53
  • 1
  • 3
  • 4
    `np.float16('2.3')` works fine for me. Can you clarify what you're asking? Perhaps show an example of what you're trying. – Mark Dickinson Oct 31 '15 at 12:57
  • related: [Floating point to 16 bit Twos Complement Binary, Python](http://stackoverflow.com/q/31464022/4279) – jfs Oct 31 '15 at 13:37
  • `float16` method doesn't convert from base 10 to base 2. For example, I want 100.25 (base 10) to IEEE754 half precision in base 2, not base 10 – Kaio Oct 31 '15 at 13:52

2 Answers2

12

if I type "117.0", it should print "0101011101010000"

>>> import numpy as np
>>> bin(np.float16(117.0).view('H'))[2:].zfill(16)
'0101011101010000'

.view('H') reinterprets the memory occupied by the float16 value as an unsigned integer.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • This solution works, thank you! I'll now try to understand your code – Kaio Oct 31 '15 at 14:34
  • 1
    @Kaio: I recommend using Python REPL, to execute the code step by step and see the intermediate results. Also use `help()`, to see the corresponding docstrings e.g., `help(''.zfill)` – jfs Oct 31 '15 at 14:39
  • @jfs could you explain the view function used here a little bit, or maybe a link to the documentation – anirudh Jan 17 '20 at 11:21
3

The float16 method suggested by Mark Dickinson has to be followed by the tostring() method to obtain the required binary representation:

data = numpy.float16(2.3).tostring()
Community
  • 1
  • 1
dlask
  • 8,776
  • 1
  • 26
  • 30
  • If I run: `import numpy as np b=np.float16(2.3).tostring() print b` , it prints only a "?@", no base 2 numbers – Kaio Oct 31 '15 at 13:49
  • The proposed solution provides the same type of result as the `struct.pack` does: A sequence of bytes. If you need a different output you have to make it clear in your question, preferably with examples. – dlask Oct 31 '15 at 13:57
  • I need to print the result of the conversion, so if I type "117.0", it should print "0101011101010000" – Kaio Oct 31 '15 at 14:07
  • 1
    @Kaio: Ah, that's helpful; thanks. Please could you edit your question to add the example? It sounds as though you want something like `'{:016b}'.format(struct.unpack(' – Mark Dickinson Oct 31 '15 at 14:11
  • @MarkDickinson Your solution would deserve to become the (accepted) answer. – dlask Oct 31 '15 at 14:15
  • @dlask: There are two good answers already! I like the `.view` approach, particularly. – Mark Dickinson Oct 31 '15 at 14:18
  • @MarkDickinson: I got a: `numpy.float16' object has no attribute 'tobytes'` – Kaio Oct 31 '15 at 14:29
  • @Kaio: Yes, sorry, the `tobytes` method is new in NumPy 1.9; you're probably using an earlier version. Use `tostring` instead. – Mark Dickinson Oct 31 '15 at 14:33
  • @MarkDickinson: Yes, I'm using NumPy 1.8.2. Now your solution works too, thank you very much Mark! – Kaio Oct 31 '15 at 14:39