[issue36934] C API Function PyLong_AsDouble Returning Wrong Value

Tim Peters report at bugs.python.org
Thu May 16 00:21:47 EDT 2019


Tim Peters <tim at python.org> added the comment:

It sounds like you need to read an introduction (any!) to computer floating-point formats.  The relevant part in the output you showed is this:

mant_dig=53

As on almost other current machines, your platform's floating point format is restricted to 53 bits of precision.  Therefore it's impossible to convert any positive integer to float without losing bits if the integer is of the form I * 2**E where I is odd and I.bit_length() > 53.

That doesn't mean integers you can convert faithfully must be "small".  For example 2**200 can be converted to float exactly.  Apart from the power-of-2-exponent, that has only 1 significant bit.  You can also convert (2**52 + 1) * 2**200 to float exactly.  But NOT (2**53 + 1) * 2**200, because that requires 54 significant bits.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36934>
_______________________________________


More information about the Python-bugs-list mailing list