struct doesn't handle NaN values?

Grant Edwards grante at visi.com
Thu May 13 17:42:00 EDT 2004


On 2004-05-13, Tim Peters <tim.one at comcast.net> wrote:

>> the struct module docs say it's IEE 754, but I can't figure
>> out how to get it to handle NaN values correctly (either
>> packing or unpacking).
>
> All Python behavior in the presence of 754 special values
> (infs, NaNs, signed zeroes) is a platform-dependent accident.

I guess the doc that claim struct supports IEEE 754 need to
have a few footnotes added.

> There's a growing list of these in PEP 42 (under
> "Non-accidental IEEE-754 support"), but nobody even bothers to
> keep that up to date.
>
>> >>> x = float('nan')
>
> It's even an accident that this line didn't raise an exception
> (it does, for example, under the Windows Python).

That would be fine.  Either the correct answer or an exception
I can handle would be acceptable.

>> >>> struct.unpack("<f",'\xff\xff\xff\xff') (-6.8056469327705772e+38,)
>
> The C routine that gets invoked here is _PyFloat_Unpack4(), in
> floatobject.c.  As the comment there says,
>
> 	/* XXX This sadly ignores Inf/NaN issues */
>
> That is, the outcome of this is also an accident.

Ah. That needs to be fixed.  It should either return a correct
value or raise an exception.  Silently returning wrong answers
is hardly the way of "least surprises".

-- 
Grant Edwards                   grante             Yow!  I'm DESPONDENT... I
                                  at               hope there's something
                               visi.com            DEEP-FRIED under this
                                                   miniature DOMED STADIUM...



More information about the Python-list mailing list