-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-120950: Fix overflow in math.log() with large int-like argument #121011
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
Open
serhiy-storchaka
wants to merge
6
commits into
python:main
Choose a base branch
from
serhiy-storchaka:math-log-int-like-overflow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−30
Open
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ca00e3b
gh-120950: Fix overflow in math.log() with large int-like argument
serhiy-storchaka 6d10cc6
Add a NEWS entry and more tests.
serhiy-storchaka 48f3e27
Merge branch 'main' into math-log-int-like-overflow
serhiy-storchaka 1d7449c
Add tests for types that have both `__float__` and `__index__`.
serhiy-storchaka c0d903f
Merge branch 'main' into math-log-int-like-overflow
serhiy-storchaka 1b491d5
Merge branch 'main' into math-log-int-like-overflow
serhiy-storchaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Library/2024-06-26-16-16-43.gh-issue-121011.qW54eh.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
:func:`math.log` now supports arbitrary large integer-like arguments in the | ||
same way as arbitrary large integer arguments. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2217,41 +2217,63 @@ math_modf_impl(PyObject *module, double x) | |||||
in that int is larger than PY_SSIZE_T_MAX. */ | ||||||
|
||||||
static PyObject* | ||||||
loghelper(PyObject* arg, double (*func)(double)) | ||||||
loghelper_int(PyObject* arg, double (*func)(double)) | ||||||
{ | ||||||
/* If it is int, do it ourselves. */ | ||||||
if (PyLong_Check(arg)) { | ||||||
double x, result; | ||||||
Py_ssize_t e; | ||||||
double x, result; | ||||||
Py_ssize_t e; | ||||||
|
||||||
/* Negative or zero inputs give a ValueError. */ | ||||||
if (!_PyLong_IsPositive((PyLongObject *)arg)) { | ||||||
PyErr_SetString(PyExc_ValueError, | ||||||
"math domain error"); | ||||||
return NULL; | ||||||
} | ||||||
/* Negative or zero inputs give a ValueError. */ | ||||||
if (!_PyLong_IsPositive((PyLongObject *)arg)) { | ||||||
PyErr_SetString(PyExc_ValueError, | ||||||
"math domain error"); | ||||||
return NULL; | ||||||
} | ||||||
|
||||||
x = PyLong_AsDouble(arg); | ||||||
if (x == -1.0 && PyErr_Occurred()) { | ||||||
if (!PyErr_ExceptionMatches(PyExc_OverflowError)) | ||||||
return NULL; | ||||||
/* Here the conversion to double overflowed, but it's possible | ||||||
to compute the log anyway. Clear the exception and continue. */ | ||||||
PyErr_Clear(); | ||||||
x = _PyLong_Frexp((PyLongObject *)arg, &e); | ||||||
if (x == -1.0 && PyErr_Occurred()) | ||||||
return NULL; | ||||||
/* Value is ~= x * 2**e, so the log ~= log(x) + log(2) * e. */ | ||||||
result = func(x) + func(2.0) * e; | ||||||
} | ||||||
else | ||||||
/* Successfully converted x to a double. */ | ||||||
result = func(x); | ||||||
return PyFloat_FromDouble(result); | ||||||
x = PyLong_AsDouble(arg); | ||||||
if (x == -1.0 && PyErr_Occurred()) { | ||||||
if (!PyErr_ExceptionMatches(PyExc_OverflowError)) | ||||||
return NULL; | ||||||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
/* Here the conversion to double overflowed, but it's possible | ||||||
to compute the log anyway. Clear the exception and continue. */ | ||||||
PyErr_Clear(); | ||||||
x = _PyLong_Frexp((PyLongObject *)arg, &e); | ||||||
if (x == -1.0 && PyErr_Occurred()) | ||||||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return NULL; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Now _PyLong_Frexp() not raises. |
||||||
/* Value is ~= x * 2**e, so the log ~= log(x) + log(2) * e. */ | ||||||
result = func(x) + func(2.0) * e; | ||||||
} | ||||||
else | ||||||
/* Successfully converted x to a double. */ | ||||||
result = func(x); | ||||||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
return PyFloat_FromDouble(result); | ||||||
} | ||||||
|
||||||
static PyObject* | ||||||
loghelper(PyObject* arg, double (*func)(double)) | ||||||
{ | ||||||
/* If it is int, do it ourselves. */ | ||||||
if (PyLong_Check(arg)) { | ||||||
return loghelper_int(arg, func); | ||||||
} | ||||||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
/* Else let libm handle it by itself. */ | ||||||
return math_1(arg, func, 0); | ||||||
PyObject *res = math_1(arg, func, 0); | ||||||
if (res == NULL && | ||||||
PyErr_ExceptionMatches(PyExc_OverflowError) && | ||||||
PyIndex_Check(arg)) | ||||||
{ | ||||||
/* Here the conversion to double overflowed, but it's possible | ||||||
to compute the log anyway. Clear the exception, convert to | ||||||
integer and continue. */ | ||||||
PyErr_Clear(); | ||||||
arg = _PyNumber_Index(arg); | ||||||
if (arg == NULL) { | ||||||
return NULL; | ||||||
} | ||||||
res = loghelper_int(arg, func); | ||||||
Py_DECREF(arg); | ||||||
} | ||||||
return res; | ||||||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
|
||||||
|
||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.