[issue11734] Add half-float (16-bit) support to struct module

Mark Dickinson report at bugs.python.org
Tue Apr 23 19:27:15 CEST 2013


Mark Dickinson added the comment:

Paul: your example works because struct.unpack('f', packed_stuff)[0] returns a regular Python float, backed by C double---i.e., exactly the same type that's returned by struct.unpack('d', packed_stuff)[0].  In effect, the single-precision C value is computed and then converted to a double-precision C value (a lossless conversion) before being wrapped in a Python float as usual.  There's no dedicated float32 type, and math.isnan and math.isinf don't have to be aware of anything other than normal Python floats.  float16 packing and unpacking would work the same way: on packing, a Python float would be converted to the closest float16 value and then serialised to a pair of bytes; on unpacking, that pair of bytes is (at least conceptually) converted to a float16 value and then to the corresponding float64 value, wrapped up in a Python float.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11734>
_______________________________________


More information about the Python-bugs-list mailing list