struct doesn't handle NaN values?

Grant Edwards grante at visi.com
Thu May 13 17:37:50 EDT 2004


On 2004-05-13, David M. Cooke <cookedm+news at physics.mcmaster.ca> 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).

> Doing this with the native format is no trouble:

> Of course, if you want to worry about endian issues here, I think
> you're SOL with using struct.

Yes, I want to worry about endian issues.  I'm communicating
via DeviceNet, and DeviceNet specifies little-endian.  I don't
want to assume the host is little-endian, so I guess I'm SOL. :(

OTOH, I suppose if I assume that 32-bit integers and floats
have the same byte order, I can use 32-bit native integers as
an intermediate format:

nanString  = struct.pack("f",float('nan'))
nanInt,    = struct.unpack("I",nanString)
extNan     = struct.pack("<I",nanInt)

That's ugly, and I'm sure will break someday, but I guess it
works for now.

I suppose I should take a look at the sources for struct and
see if I can fix it...

-- 
Grant Edwards                   grante             Yow!  How's it going in
                                  at               those MODULAR LOVE UNITS??
                               visi.com            



More information about the Python-list mailing list