Extracting bit fields from an IEEE-784 float
ulrich.eckhardt at dominolaser.com
Mon Jul 30 08:42:58 CEST 2012
Am 30.07.2012 02:44, schrieb Steven D'Aprano:
> I wish to extract the bit fields from a Python float, call it x. First I
> cast the float to 8-bytes:
> s = struct.pack('=d', x)
> i = struct.unpack('=q', s)
> Then I extract the bit fields from the int, e.g. to grab the sign bit:
> (i & 0x8000000000000000) >> 63
> 1) Are there any known implementations or platforms where Python floats
> are not C doubles? If so, what are they?
The struct docs refer to C's double type, so it depends on that type
probably. However, regardless of C's double type, the same docs refer to
the IEEE form when packed into a byte array. Is it just the
representation you are after or some specific behaviour?
> 2) If the platform byte-order is reversed, do I need to take any special
> action? I don't think I do, because even though the float is reversed, so
> will be the bit mask. Is this correct?
Yes, the code is fine. If you have doubts, I have a big-endian system at
home (Linux/PowerPC) where I could run tests.
> 3) Any other problems with the way I am doing this?
Python docs refer to IEEE-754, not 784? Typo?
More information about the Python-list