This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: memoryview lacks support for half floats
Type: enhancement Stage: needs patch
Components: Interpreter Core Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: corona10, mark.dickinson, meador.inge, pitrou
Priority: normal Keywords:

Created on 2022-01-31 15:29 by pitrou, last changed 2022-04-11 14:59 by admin.

Messages (1)
msg412205 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2022-01-31 15:28
The struct module has support for half-floats (the "e" format code) but support is not fully enabled in the memoryview object.

Let's contrast float32 (the "f" format code), which you can cast to, and read as Python objects:

>>> a = np.array([0.0, -1.5], np.float32())
>>> list(memoryview(a))
[0.0, -1.5]
>>> memoryview(a.tobytes()).cast('f').tolist()
[0.0, -1.5]

and float16, where support is minimal (casting forbidden, reading as Python objects unimplemented):

>>> a = np.array([0.0, -1.5], np.float16())
>>> list(memoryview(a))
Traceback (most recent call last):
  File "<ipython-input-15-102982f8ac8e>", line 1, in <module>
    list(memoryview(a))
NotImplementedError: memoryview: format e not supported

>>> memoryview(a.tobytes()).cast('e').tolist()
Traceback (most recent call last):
  File "<ipython-input-25-78df215a7360>", line 1, in <module>
    memoryview(a.tobytes()).cast('e').tolist()
ValueError: memoryview: destination format must be a native single character format prefixed with an optional '@'
History
Date User Action Args
2022-04-11 14:59:55adminsetgithub: 90751
2022-01-31 15:54:50corona10setnosy: + corona10
2022-01-31 15:29:00pitroucreate