Skip to content

Commit 6dbe9e4

Browse files
authored
Merge pull request #269 from vchamarthi/fix/absolute-nan
BUG: fix absolute() returning -NaN for +NaN on scalar path
2 parents 118b179 + 316e22f commit 6dbe9e4

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
* Pinned Cython in the Coverity Scan workflow so generated code stays stable between scans, and added `coverity/README.md` documenting the known Cython-boilerplate false positives and the scan review checklist [gh-266](https://github.com/IntelPython/mkl_umath/pull/266)
1515

1616
### Fixed
17+
* Fixed `absolute` (float32/float64) returning `-NaN` for `+NaN` input on the scalar fallback path (same issue as numpy/numpy@dc478c58b9, gh-31433) [gh-269](https://github.com/IntelPython/mkl_umath/pull/269)
1718
* Fixed an over-decref of the borrowed module dictionary reference on the module initialization error path [gh-264](https://github.com/IntelPython/mkl_umath/pull/264)
1819
* Fixed a leak of the module object when the NumPy C-API import fails during module initialization [gh-263](https://github.com/IntelPython/mkl_umath/pull/263)
1920

‎mkl_umath/src/mkl_umath_loops.c.src‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ mkl_umath_@TYPE@_absolute(char **args, const npy_intp *dimensions, const npy_int
917917
else {
918918
UNARY_LOOP {
919919
const @type@ in1 = *(@type@ *)ip1;
920-
const @type@ tmp = in1 > 0 ? in1 : -in1;
920+
const @type@ tmp = fabs@c@(in1);
921921
/* add 0 to clear -0.0 */
922922
*((@type@ *)op1) = tmp + 0;
923923
}

‎mkl_umath/tests/test_basic.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,16 @@ def test_reduce_complex(func, dtype):
189189
assert np.allclose(
190190
mkl_res, np_res
191191
), f"Results for '{func}[reduce]' do not match"
192+
193+
194+
@pytest.mark.parametrize("size", [100, 8192 + 1])
195+
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
196+
def test_absolute_nan_signbit(size, dtype):
197+
# size 100 takes the fallback loop, 8193 takes MKL's vAbs
198+
for nan in (np.nan, -np.nan):
199+
a = np.full(size, nan, dtype=dtype)
200+
sign = "-" if np.signbit(nan) else "+"
201+
assert not np.signbit(mu.absolute(a)).any(), (
202+
f"absolute({sign}nan) must be +nan for "
203+
f"{dtype.__name__} at size {size}"
204+
)

0 commit comments

Comments
 (0)