[issue4114] struct returns incorrect 4 byte float
Martin v. Löwis
report at bugs.python.org
Tue Oct 14 00:36:06 CEST 2008
Martin v. Löwis <martin at v.loewis.de> added the comment:
> Don't hesitate to reopen the bug if you consider that
> something should be fixed in Python.
I agree that it should be closed; people should read general CS
introductory material to learn how floating point numbers work.
> @loewis: Yes, the initial problem is about unpack("f", bytes). It's
> not possible to exact original 32 bits float value
Interestingly enough, it is possible - using the OPs approach.
If you want to truncate a 64-bit floating point number to a
32-bit one, pack it as float in the struct module, then unpack it.
Python will automatically pad the mantissa bytes with null bytes.
> because Python
> forces a conversion to 64 bits float. The behaviour should be
I think it's documented somewhere that a Python float is represented
with a C double. That should suffice, IMO.
Python tracker <report at bugs.python.org>
More information about the Python-bugs-list