[Python-ideas] Exact integral types in struct
Andrew Svetlov
andrew.svetlov at gmail.com
Tue Mar 20 21:36:02 CET 2012
Sorry, my fault.
But as you can see json lib switches byteorder manually - so it's not an error.
Obviously it will be cleaner to use direct '>d' form.
Please make an issue in bugtracker if you want.
On Tue, Mar 20, 2012 at 10:27 PM, Serhiy Storchaka <storchaka at gmail.com> wrote:
> 20.03.12 21:54, Andrew Svetlov написав(ла):
>
>> Floating points from IEEE 754 doesn't depends from machine byte order
>> and C double is always coded in 8 bytes as I know,
>
>
> Full code:
>
> def _floatconstants():
> _BYTES = binascii.unhexlify(b'7FF80000000000007FF0000000000000')
> if sys.byteorder != 'big':
> _BYTES = _BYTES[:8][::-1] + _BYTES[8:][::-1]
> nan, inf = struct.unpack('dd', _BYTES)
> return nan, inf, -inf
>
> NaN, PosInf, NegInf = _floatconstants()
>
>
> But in xdrlib.py:
>
> return struct.unpack('>d', data)[0]
>
> And in pickle.py:
>
> self.append(unpack('>d', self.read(8))[0])
>
> Test:
>
> >>> import struct
> >>> struct.pack('>d', 1)
> b'?\xf0\x00\x00\x00\x00\x00\x00'
> >>> struct.pack('<d', 1)
> b'\x00\x00\x00\x00\x00\x00\xf0?'
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
--
Thanks,
Andrew Svetlov
More information about the Python-ideas
mailing list