Skip to content

Commit 6e5aee3

Browse files
author
Release Manager
committed
gh-39135: Fix ginac's ratlog method so log(int(0),2) does not hang Fixes #37794. It is explained in #37794 that calculating the logarithm of `int(0)` (zero represented as a python integer) can enter an infinite loop, due to a bug in ginac's `numeric::ratlog` method. This PR adds an `if` statement to return negative infinity for this value. (At this point in the code, the base of the logarithm is known to be a positive integer that is greater than 1, so the log of 0 is negative infinity, not positive infinity.) The PR also adds `log(int(0), 2)` as a doctest and, for good measure, also `log(int(0), 1/2)`. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - #12345: short description why this is a dependency --> <!-- - #34567: ... --> URL: #39135 Reported by: DaveWitteMorris Reviewer(s): Lorenz Panny
2 parents ebc3265 + f9a3b43 commit 6e5aee3

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/sage/misc/functional.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,13 @@ def log(*args, **kwds):
11371137
11381138
sage: log(0, 2)
11391139
-Infinity
1140+
1141+
Check if :issue:`37794` is fixed::
1142+
1143+
sage: log(int(0), 2)
1144+
-Infinity
1145+
sage: log(int(0), 1/2)
1146+
+Infinity
11401147
"""
11411148
base = kwds.pop('base', None)
11421149
if base:

src/sage/symbolic/ginac/numeric.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3665,7 +3665,7 @@ const numeric numeric::log(const numeric &b, PyObject* parent) const {
36653665
}
36663666

36673667
// General log
3668-
// Handle special cases here that return MPZ/MPQ
3668+
// Handle special cases here that return MPZ/MPQ (or an infinity)
36693669
const numeric numeric::ratlog(const numeric &b, bool& israt) const {
36703670
israt = true;
36713671
if (b.is_one()) {
@@ -3688,6 +3688,9 @@ const numeric numeric::ratlog(const numeric &b, bool& israt) const {
36883688
israt = false;
36893689
return *_num0_p;
36903690
}
3691+
if (v._long == 0) {
3692+
return py_funcs.py_eval_neg_infinity();
3693+
}
36913694
int c = 0;
36923695
std::ldiv_t ld;
36933696
ld.quot = v._long;

0 commit comments

Comments
 (0)