Skip to content

Commit 86a2566

Browse files
committed
MNT: review comments
Signed-off-by: Iason Krommydas <[email protected]>
1 parent 1138de4 commit 86a2566

4 files changed

Lines changed: 46 additions & 53 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
``numpy.unwrap`` behavior changes for edge cases
2+
-------------------------------------------------
3+
An explicitly typed ``discont`` argument passed to `numpy.unwrap` wider than the
4+
result dtype is now compared at the result dtype rather than promoting. This may
5+
change the result of `numpy.unwrap` by ~1 ULP.
6+
7+
Calling `numpy.unwrap` with an unsigned integer ``period`` that cannot represent
8+
the values needed internally now raises a `TypeError` ("no loop found" ufunc
9+
error, reporting the mismatched dtypes) instead of the ``OverflowError``
10+
raised by the previous Python implementation.

‎numpy/_core/src/umath/unwrap.cpp‎

Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#define PY_SSIZE_T_CLEAN
1010
#include <Python.h>
1111

12-
#include <limits>
1312
#include <type_traits>
1413

1514
#include "numpy/halffloat.h"
@@ -63,19 +62,18 @@ floor_mod(npy_longdouble a, npy_longdouble b)
6362
#endif
6463

6564
/* no integer npy_remainder to call; mirrors @TYPE@_remainder in
66-
* loops_modulo.dispatch.c.src, including the T_MIN / -1 guard */
65+
* loops_modulo.dispatch.c.src, including the T_MIN / -1 guard (which sets
66+
* no floating point status there either -- only the quotient overflows,
67+
* and this only ever computes the remainder) */
6768
template <typename T>
68-
static inline std::enable_if_t<std::is_integral_v<T>, T>
69+
static inline std::enable_if_t<std::is_integral_v<T> && std::is_signed_v<T>, T>
6970
floor_mod(T a, T b)
7071
{
7172
if (b == 0) {
7273
npy_set_floatstatus_divbyzero();
7374
return 0;
7475
}
7576
if (b == -1) {
76-
if (a == std::numeric_limits<T>::min()) {
77-
npy_set_floatstatus_divbyzero();
78-
}
7977
return 0;
8078
}
8179
T rem = a % b;
@@ -86,17 +84,18 @@ floor_mod(T a, T b)
8684
}
8785

8886
/*
89-
* One loop per (T, D) pair. T is the storage/compute type; D is discont's
90-
* type, either T itself (NEP 50 weak-scalar match) or a wider float type
91-
* for explicitly-typed/cross-kind discont. npy_half has no native
92-
* arithmetic, so it gets its own loop below.
87+
* One loop per real dtype. T is the storage/compute type; discont is read
88+
* as T itself for the float dtypes, npy_double for the integer ones.
89+
* npy_half has no native arithmetic, so it gets its own loop below.
9390
*/
94-
template <typename T, typename D>
91+
template <typename T>
9592
static int
9693
unwrap_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
9794
npy_intp const dimensions[], npy_intp const strides[],
9895
NpyAuxData *NPY_UNUSED(auxdata))
9996
{
97+
using D = std::conditional_t<std::is_floating_point_v<T>, T, npy_double>;
98+
10099
npy_intp n_outer = dimensions[0];
101100
npy_intp n = dimensions[1];
102101
if (n == 0) {
@@ -160,7 +159,6 @@ unwrap_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
160159
static inline float h2f(npy_half h) { return npy_half_to_float(h); }
161160
static inline npy_half f2h(float f) { return npy_float_to_half(f); }
162161

163-
template <typename D>
164162
static int
165163
unwrap_half_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
166164
npy_intp const dimensions[], npy_intp const strides[],
@@ -180,15 +178,7 @@ unwrap_half_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
180178

181179
for (npy_intp k = 0; k < n_outer; k++,
182180
p_o += s_p, disc_o += s_disc, per_o += s_per, out_o += s_out) {
183-
/* npy_half discont is read through h2f, matching the NEP 50 weak-
184-
* scalar case where discont is compared at half precision */
185-
double discont;
186-
if constexpr (std::is_same_v<D, npy_half>) {
187-
discont = h2f(*(const npy_half *)disc_o);
188-
}
189-
else {
190-
discont = *(const D *)disc_o;
191-
}
181+
double discont = h2f(*(const npy_half *)disc_o);
192182
npy_half period = *(const npy_half *)per_o;
193183
npy_half interval_high = f2h(h2f(period) / 2.0f);
194184
npy_half interval_low = f2h(-h2f(interval_high));
@@ -224,13 +214,15 @@ unwrap_half_loop(PyArrayMethod_Context *NPY_UNUSED(context), char *const data[],
224214

225215

226216
static int
227-
add_unwrap_loop(PyObject *ufunc, int typenum,
228-
PyArrayMethod_StridedLoop *loop, int discont_typenum)
217+
add_unwrap_loop(PyObject *ufunc, int typenum, PyArrayMethod_StridedLoop *loop)
229218
{
230219
PyArray_DTypeMeta *dt = PyArray_DTypeFromTypeNum(typenum);
231220
if (dt == NULL) {
232221
return -1;
233222
}
223+
/* discont is read at typenum's own precision for float dtypes, double
224+
* for the integer ones -- matches unwrap_loop's own D derivation */
225+
int discont_typenum = PyTypeNum_ISFLOAT(typenum) ? typenum : NPY_DOUBLE;
234226
PyArray_DTypeMeta *disc_dt = PyArray_DTypeFromTypeNum(discont_typenum);
235227
if (disc_dt == NULL) {
236228
Py_DECREF(dt);
@@ -274,36 +266,24 @@ init_unwrap_ufunc(PyObject *umath)
274266
struct Loop {
275267
int typenum;
276268
PyArrayMethod_StridedLoop *loop;
277-
int discont_typenum;
278269
};
279270

280-
/*
281-
* float dtypes get two loops: discont at native precision (the NEP 50
282-
* weak-scalar case, where the python wrapper matches discont to p's
283-
* dtype) and at wider precision (explicitly-typed/cross-kind discont).
284-
* Integer dtypes only get the wider loop -- NEP 50 native-int discont
285-
* is not implemented, so the wrapper always widens for those.
286-
*/
287271
Loop loops[] = {
288-
{NPY_HALF, unwrap_half_loop<npy_half>, NPY_HALF},
289-
{NPY_HALF, unwrap_half_loop<npy_double>, NPY_DOUBLE},
290-
{NPY_FLOAT, unwrap_loop<npy_float, npy_float>, NPY_FLOAT},
291-
{NPY_FLOAT, unwrap_loop<npy_float, npy_double>, NPY_DOUBLE},
292-
{NPY_DOUBLE, unwrap_loop<npy_double, npy_double>, NPY_DOUBLE},
293-
{NPY_LONGDOUBLE, unwrap_loop<npy_longdouble, npy_longdouble>,
294-
NPY_LONGDOUBLE},
295-
{NPY_BYTE, unwrap_loop<npy_byte, npy_double>, NPY_DOUBLE},
296-
{NPY_SHORT, unwrap_loop<npy_short, npy_double>, NPY_DOUBLE},
297-
{NPY_INT, unwrap_loop<npy_int, npy_double>, NPY_DOUBLE},
298-
{NPY_LONG, unwrap_loop<npy_long, npy_double>, NPY_DOUBLE},
299-
{NPY_LONGLONG, unwrap_loop<npy_longlong, npy_double>, NPY_DOUBLE},
272+
{NPY_HALF, unwrap_half_loop},
273+
{NPY_FLOAT, unwrap_loop<npy_float>},
274+
{NPY_DOUBLE, unwrap_loop<npy_double>},
275+
{NPY_LONGDOUBLE, unwrap_loop<npy_longdouble>},
276+
{NPY_BYTE, unwrap_loop<npy_byte>},
277+
{NPY_SHORT, unwrap_loop<npy_short>},
278+
{NPY_INT, unwrap_loop<npy_int>},
279+
{NPY_LONG, unwrap_loop<npy_long>},
280+
{NPY_LONGLONG, unwrap_loop<npy_longlong>},
300281
/* object dtype falls back to the python implementation instead */
301282
// {NPY_OBJECT, object_unwrap_loop},
302283
};
303284

304285
for (const auto& entry : loops) {
305-
if (add_unwrap_loop(ufunc, entry.typenum, entry.loop,
306-
entry.discont_typenum) < 0) {
286+
if (add_unwrap_loop(ufunc, entry.typenum, entry.loop) < 0) {
307287
Py_DECREF(ufunc);
308288
return -1;
309289
}

‎numpy/lib/_function_base_impl.py‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,15 +1872,9 @@ def unwrap(p, discont=None, axis=-1, *, period=2 * pi):
18721872
p = asanyarray(p)
18731873
if discont is None:
18741874
discont = period / 2
1875-
dtype = np.result_type(p, period)
1876-
# match NEP 50: a discont that would collapse to dtype anyway (the
1877-
# weak-scalar or same-dtype case) is compared at that native precision;
1878-
# anything wider (explicit stronger scalar, or cross-kind on an integer
1879-
# dtype) is compared at >=double
1880-
discont_type = np.longdouble if dtype.type is np.longdouble else np.float64
1881-
if _nx.issubdtype(dtype, _nx.floating) and np.result_type(dtype, discont) == dtype:
1882-
discont_type = dtype.type
18831875
try:
1876+
dtype = np.result_type(p, period)
1877+
discont_type = type(dtype) if _nx.issubdtype(dtype, _nx.floating) else np.float64
18841878
return _unwrap(p, discont, period,
18851879
signature=(type(dtype), discont_type, type(dtype), type(dtype)),
18861880
axis=axis)

‎numpy/lib/tests/test_function_base.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,6 +2478,15 @@ def test_discont_weak_scalar_narrow_dtype(self):
24782478
discont = 3 + 1e-4
24792479
assert_array_equal(unwrap(p, discont=discont, period=period), [0, -1])
24802480

2481+
def test_discont_explicit_wider_truncates_to_dtype(self):
2482+
# an explicitly typed discont wider than the result dtype is
2483+
# compared at the result dtype rather than promoting (see release
2484+
# notes); 3 + 1e-8 rounds down to exactly 3.0 in float32
2485+
p = np.array([0, 3], dtype=np.float32)
2486+
period = 4
2487+
discont = np.float64(3) + 1e-8
2488+
assert_array_equal(unwrap(p, discont=discont, period=period), [0, -1])
2489+
24812490
def test_masked_array(self):
24822491
# for masked arrays, we'd need a masked aware np.ma.unwrap implementation
24832492
# the operation currently unmasks the masked array into an ndarray

0 commit comments

Comments
 (0)