Skip to content

Commit ae4c396

Browse files
committed
+ restore _Py_ADJUST_ERANGE2
1 parent 8da530c commit ae4c396

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

Include/internal/pycore_pymath.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ extern "C" {
1010

1111

1212
/* _Py_ADJUST_ERANGE1(x)
13-
* Set errno to 0 before calling a libm function, and invoke macro after.
14-
* This makes two kinds of
13+
* _Py_ADJUST_ERANGE2(x, y)
14+
* Set errno to 0 before calling a libm function, and invoke one of these
15+
* macros after, passing the function result(s) (_Py_ADJUST_ERANGE2 is useful
16+
* for functions returning complex results). This makes two kinds of
1517
* adjustments to errno: (A) If it looks like the platform libm set
1618
* errno=ERANGE due to underflow, clear errno. (B) If it looks like the
1719
* platform libm overflowed but didn't set errno, force errno to ERANGE. In
@@ -40,6 +42,20 @@ static inline void _Py_ADJUST_ERANGE1(double x)
4042
}
4143
}
4244

45+
static inline void _Py_ADJUST_ERANGE2(double x, double y)
46+
{
47+
if (x == INFINITY || x == -INFINITY ||
48+
y == INFINITY || y == -INFINITY)
49+
{
50+
if (errno == 0) {
51+
errno = ERANGE;
52+
}
53+
}
54+
else if (errno == ERANGE) {
55+
errno = 0;
56+
}
57+
}
58+
4359

4460
//--- HAVE_PY_SET_53BIT_PRECISION macro ------------------------------------
4561
//

Objects/complexobject.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "pycore_freelist.h" // _Py_FREELIST_FREE(), _Py_FREELIST_POP()
1212
#include "pycore_long.h" // _PyLong_GetZero()
1313
#include "pycore_object.h" // _PyObject_Init()
14+
#include "pycore_pymath.h" // _Py_ADJUST_ERANGE2()
1415

1516

1617
#define _PyComplexObject_CAST(op) ((PyComplexObject *)(op))
@@ -325,7 +326,7 @@ _Py_c_pow(Py_complex a, Py_complex b)
325326
r.imag = len*sin(phase);
326327

327328
if (_Py_c_isinf(r) && _Py_c_isfinite(a) && _Py_c_isfinite(b)) {
328-
errno = ERANGE;
329+
_Py_ADJUST_ERANGE2(r.real, r.imag);
329330
}
330331
}
331332
return r;
@@ -747,7 +748,7 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
747748
if (b.imag == 0.0 && b.real == floor(b.real) && fabs(b.real) <= 100.0) {
748749
p = c_powi(a, (long)b.real);
749750
if (_Py_c_isinf(p) && _Py_c_isfinite(a) && isfinite(b.real)) {
750-
errno = ERANGE;
751+
_Py_ADJUST_ERANGE2(p.real, p.imag);
751752
}
752753
}
753754
else {

0 commit comments

Comments
 (0)