Skip to content

Commit a858964

Browse files
committed
gh-156695: Improve accuracy of complex div and pow
1 parent 1414d2a commit a858964

4 files changed

Lines changed: 301 additions & 137 deletions

File tree

Include/internal/pycore_complexobject.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,29 @@ PyAPI_FUNC(Py_complex) _Py_cr_prod(Py_complex, double);
2727
PyAPI_FUNC(Py_complex) _Py_cr_quot(Py_complex, double);
2828
PyAPI_FUNC(Py_complex) _Py_rc_quot(double, Py_complex);
2929

30+
static inline bool
31+
_Py_c_isnan(Py_complex a)
32+
{
33+
return isnan(a.real) || isnan(a.imag);
34+
}
35+
36+
static inline bool
37+
_Py_c_isinf(Py_complex a)
38+
{
39+
return isinf(a.real) || isinf(a.imag);
40+
}
41+
42+
static inline bool
43+
_Py_c_isfinite(Py_complex a)
44+
{
45+
return isfinite(a.real) && isfinite(a.imag);
46+
}
47+
48+
static inline bool
49+
_Py_c_iszero(Py_complex a)
50+
{
51+
return a.real == 0.0 && a.imag == 0.0;
52+
}
3053

3154
#ifdef __cplusplus
3255
}

Include/internal/pycore_pymath.h

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

1111

1212
/* _Py_ADJUST_ERANGE1(x)
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
13+
* Set errno to 0 before calling a libm function, and invoke this
14+
* macro after, passing the function result(s).
15+
* This makes two kinds of
1716
* adjustments to errno: (A) If it looks like the platform libm set
1817
* errno=ERANGE due to underflow, clear errno. (B) If it looks like the
1918
* platform libm overflowed but didn't set errno, force errno to ERANGE. In
@@ -42,20 +41,6 @@ static inline void _Py_ADJUST_ERANGE1(double x)
4241
}
4342
}
4443

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-
5944

6045
//--- HAVE_PY_SET_53BIT_PRECISION macro ------------------------------------
6146
//
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Improve accuracy of complex division and complex powers with small negative
2+
integer exponents. This also applies to :c:func:`_Py_c_quot` and
3+
:c:func:`_Py_c_pow`. Contributed by High Performance Kernels LLC.

0 commit comments

Comments
 (0)