-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-135487: fix reprlib.Repr.repr_int
when given very large integers
#135506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
gh-135487: fix reprlib.Repr.repr_int
when given very large integers
#135506
Conversation
a50f94a
to
4ac21bb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that we can try to reproduce the result of old Python versions, before introducing the limitation on maximal digit count.
If the length exceeds maxlong
, repr_int()
returns the first i
digits followed by fillvalue
followed by the last j
digits. I afraid that calculating the last j
digits has the same complexity as calculating all digits. But for the first i
digits it may be faster.
We could also use 10**math.floor(math.log(x))
to get first digits. There are several caveats. Firs, the precision of float is limited, and the larger x
, the less significant digits we can get. But it still can be more than 10 digits in most practical cases. We can estimate the error and the number of reliable digits. Second, even with best effort, it can give wrong even the first digit -- it is difficult to differentiate 9999...
from 10000...
. In this case we should calculate the first i
digits, multiply them by 100**(k-i)
, and then calculate the correction. This can be repeated iteratively, so we can get arbitrary number of first digits.
cc @tim-one
@@ -219,7 +219,6 @@ which format specific object types. | |||
|
|||
|
|||
.. method:: Repr.repr_TYPE(obj, level) | |||
:noindex: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove this? repr_TYPE
is not a real method name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was to make a link in the NEWS entry.
Lib/reprlib.py
Outdated
# When math.log10(abs(x)) is overestimated or underestimated, | ||
# the number of digits should be k - 1 or k + 1 respectively. | ||
# For simplicity, we do not compute the exact number of digits. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can differ more than by 1 for large numbers with more than 2**53
digits. Yes, it will take long time (years?) to calculate the decimal digits, but let be more future proof.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, I actually didn't consider the possibility of having 2**53 digits...
Getting the number of digits may be wrong due to some round-off errors but I don't want to overcomplicate the code.
reprlib.repr
fails for very large numbers #135487