kutsurak / python-bitstring

Automatically exported from code.google.com/p/python-bitstring
0 stars 0 forks source link

bitstring.Bits "round robin" bin -> float -> bin conversion does not preserve NaN bit #140

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
from bitstring import Bits

Bits (float=Bits (bin='11111111100000000000000000000001').float, length=32).bin

The expected output is: 11111111100000000000000000000001
The actual output is:   11111111110000000000000000000001
                                 ^
                                 MSB fraction set bit
MS Windows 7 ultimate 64-bit
Python v2.7.3 64-bit
bitstring v3.1.2

The conversion involves a NaN floating-point pattern.  Apparently, any NaN
"round robin" conversion always leaves the MSB fraction bit set.  Note that
this issue does not occur with float patterns of length 64 so perhaps it is
an internal rounding issue.

Original issue reported on code.google.com by 944725BB...@gmail.com on 25 Nov 2013 at 7:48

GoogleCodeExporter commented 9 years ago
It's interesting, but I don't see it as an issue for bitstring, it's just 
because there's a many to one mapping of bitstrings to floats, which isn't 
anything I can affect...

Both bitstrings evaluate to 'nan' as a float, and as the conversion goes via a 
float there's no way to keep the information as to which bitstring created the 
float to start with.

'nan's are strange things, and the multiplicity of interpretations is most 
easily shown with the lines:

>>> a = float('nan')
>>> a == a
False

Closing the bug as there's nothing I could ever do to save that bit - we're at 
the mercy of the floating point interpretation in Python.

Original comment by dr.scott...@gmail.com on 26 Nov 2013 at 12:14