Skip to content

Commit 8e0c482

Browse files
committed
Polishing of c_powu(): exclude unreachible cases and magic numbers
1 parent 70d21c7 commit 8e0c482

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

Objects/complexobject.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,16 @@ _Py_c_pow(Py_complex a, Py_complex b)
156156
return r;
157157
}
158158

159+
#define INT_EXP_CUTOFF 100
160+
159161
static Py_complex
160162
c_powu(Py_complex x, long n)
161163
{
162164
Py_complex p = x, r = n-- ? p : c_1;
163165
long mask = 1;
164-
while (mask > 0 && n >= mask) {
166+
assert(-1 <= n && n <= INT_EXP_CUTOFF);
167+
while (n >= mask) {
168+
assert(mask>0);
165169
if (n & mask)
166170
r = _Py_c_prod(r,p);
167171
mask <<= 1;
@@ -541,7 +545,7 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
541545
errno = 0;
542546
// Check whether the exponent has a small integer value, and if so use
543547
// a faster and more accurate algorithm.
544-
if (b.imag == 0.0 && b.real == floor(b.real) && fabs(b.real) <= 100.0) {
548+
if (b.imag == 0.0 && b.real == floor(b.real) && fabs(b.real) <= INT_EXP_CUTOFF) {
545549
p = c_powi(a, (long)b.real);
546550
}
547551
else {

0 commit comments

Comments
 (0)