struct doesn't handle NaN values?

John Roth newsgroups at jhrothjr.com
Thu May 13 17:59:49 EDT 2004


"Grant Edwards" <grante at visi.com> wrote in message
news:slrnca7q3u.bdf.grante at grante.rivatek.com...
> On 2004-05-13, John Roth <newsgroups at jhrothjr.com> wrote:
>
> >> Perhaps I'm doing something wrong: 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).
>
> > I believe that struct uses the c library as is,
>
> That's not what the docs say. The struct docs says it converts
> between native Python values and IEEE 754.  If that's not the
> case and it converts between Python float format and "native C
> library" format, then the docs need to be changed [and I'll
> need to write something that converts to/from IEEE 754 format.]

Actually, it doesn't. It does say IEEE, but it doesn't say
IEEE 754. While that's a nit, it also doesn't say it converts
anything other than byte order. When you look at the source,
I suspect that what you'll find is a straight copy from the buffer
into wherever the float object keeps the actual value, or the
reverse, of course.
>
> > like the float support in general. Unfortunately, that leaves
> > everyone at the mercy of the rather inconsistent and
> > ideosyncratic implementations of corner cases in existing c
> > libraries.
>
> But, my c library seems to handle it correctly:
>
>  1) 0xffffffff is treated correctly as a NaN.
>  2) NaN's are detected and converted to strings as 'nan'.
>
> Which part of the C library is broken?

Which C library? Python runs on 20 different systems,
many of which have multiple operating systems, each
of which has its own C library with its own problems.
It's not even a standards issue: the older standards didn't
specify what the C library's conversion routines should do.

I referred you to PEP 754 for a reason. That PEP contains
a thorough discussion of the issues in the treatment of special
values in Python floating point.

http://www.python.org/peps/pep-0754.html

It also contains a reference to a module that handles
the matter.


John Roth
>
> -- 
> Grant Edwards                   grante             Yow!  AIEEEEE! I am
having
>                                   at               an UNDULATING
EXPERIENCE!
>                                visi.com





More information about the Python-list mailing list