[New-bugs-announce] [issue45032] struct.unpack() returns NaN

Jorge Rojas report at bugs.python.org
Fri Aug 27 13:13:38 EDT 2021


New submission from Jorge Rojas <jrojas at we-techs.com>:

Hi all!

I have this case when trying to get a float value applying pack to these integer values:
struct.unpack('f', struct.pack('HH', 0, 32704))

This happens when executing the unpack function to a float format, from a bit-array where the sign bit is not in a suitable position I think. Applying big-endian to the format, it returns a numeric value, but being little-endian it returns a NaN.

> struct.unpack('<f', struct.pack('HH',0, 32704))
Out[168]: (nan,)
> struct.unpack('>f', struct.pack('HH',0, 32704))
Out[169]: (6.905458702346266e-41,)

The current documentation on struct.unpack doesn't anything about what conditions a NaN is returned, besides this might be a expected value. Maybe explaining how this value could be converted to an equivalent format to retrieve the proper value may help, or why this returns a NaN and how to avoid it.

Thanks in advance.

----------
components: Library (Lib)
messages: 400434
nosy: jrojas
priority: normal
severity: normal
status: open
title: struct.unpack() returns NaN
type: behavior
versions: Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45032>
_______________________________________


More information about the New-bugs-announce mailing list