eryksun added the comment:
int(float_version) is returning the actual integer value represented by the
float, which is the closest approximation possible using an [IEEE 754
binary64][1], i.e. a C double. Here's one way to compute this value manually:
from struct import pack
from fractions import Fraction
some_number = 1 * 10**44
float_version = float(some_number)
n = int.from_bytes(pack('>d', float_version), 'big')
sign = n >> 63
exp = ((n >> 52) & (2**11 - 1)) - 1023
nmantissa = n & (2**52 - 1)
significand = 1 + sum(Fraction((nmantissa >> (52 - i)) & 1, 2**i)
for i in range(52))
>>> (-1)**sign * significand * 2**exp
Fraction(100000000000000008821361405306422640701865984, 1)
Or just use the as_integer_ratio method:
>>> float_version.as_integer_ratio()
(100000000000000008821361405306422640701865984, 1)
[1]: https://en.wikipedia.org/wiki/Double-precision_floating-point_format
----------
nosy: +eryksun
resolution: -> not a bug
stage: -> resolved
status: open -> closed
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue25358>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com