struct doesn't handle NaN values?

John Roth newsgroups at jhrothjr.com
Thu May 13 18:11:22 EDT 2004


"Grant Edwards" <grante at visi.com> wrote in message
news:slrnca7qt2.bdf.grante at grante.rivatek.com...
> 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".

A lot of people would like the whole mess to be fixed. Unfortunately,
nobody has stepped forward to do it, considering that it's quite
a job to do right. Given that there's probably a bunch of stuff out there
that depends on the current system dependent idiosyncracies,
it would have to be done with the "from future import ..." multi-release
phase in.

Now that I've found the reference module, I'll probably put it
into PyFIT in the next few days, but that simply patches the
problem for one application.

John Roth
>
> -- 
> 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