diff options
author | Antoine Pitrou <antoine@python.org> | 2015-04-23 11:44:02 +0200 |
---|---|---|
committer | Antoine Pitrou <antoine@python.org> | 2015-04-23 11:47:31 +0200 |
commit | fb93254e97e04a99c451701a9177468866eadd93 (patch) | |
tree | 1140c100ceb464f392a289dc8fd0dca5dc8d272b | |
parent | 02b858326dac217607a83ed0bf4d7d51d5bfbfbe (diff) | |
download | numpy-fb93254e97e04a99c451701a9177468866eadd93.tar.gz |
BUG: fix abs() of floating point scalar on negative zeros
Closes #5781.
-rw-r--r-- | numpy/core/src/umath/scalarmath.c.src | 18 | ||||
-rw-r--r-- | numpy/core/tests/test_scalarmath.py | 24 |
2 files changed, 38 insertions, 4 deletions
diff --git a/numpy/core/src/umath/scalarmath.c.src b/numpy/core/src/umath/scalarmath.c.src index e2c8137b3..e4fc617a5 100644 --- a/numpy/core/src/umath/scalarmath.c.src +++ b/numpy/core/src/umath/scalarmath.c.src @@ -485,10 +485,8 @@ static void /**begin repeat - * #name = byte, short, int, long, longlong, - * float, double, longdouble# - * #type = npy_byte, npy_short, npy_int, npy_long, npy_longlong, - * npy_float, npy_double, npy_longdouble# + * #name = byte, short, int, long, longlong# + * #type = npy_byte, npy_short, npy_int, npy_long, npy_longlong# */ static void @name@_ctype_absolute(@type@ a, @type@ *out) @@ -497,6 +495,18 @@ static void } /**end repeat**/ +/**begin repeat + * #name = float, double, longdouble# + * #type = npy_float, npy_double, npy_longdouble# + * #c = f,,l# + */ +static void +@name@_ctype_absolute(@type@ a, @type@ *out) +{ + *out = npy_fabs@c@(a); +} +/**end repeat**/ + static void half_ctype_absolute(npy_half a, npy_half *out) { diff --git a/numpy/core/tests/test_scalarmath.py b/numpy/core/tests/test_scalarmath.py index 3ba3beff9..8b6816958 100644 --- a/numpy/core/tests/test_scalarmath.py +++ b/numpy/core/tests/test_scalarmath.py @@ -11,6 +11,9 @@ types = [np.bool_, np.byte, np.ubyte, np.short, np.ushort, np.intc, np.uintc, np.single, np.double, np.longdouble, np.csingle, np.cdouble, np.clongdouble] +floating_types = np.floating.__subclasses__() + + # This compares scalarmath against ufuncs. class TestTypes(TestCase): @@ -284,5 +287,26 @@ class TestSizeOf(TestCase): assert_raises(TypeError, d.__sizeof__, "a") +class TestAbs(TestCase): + + def _test_abs_func(self, absfunc): + for tp in floating_types: + x = tp(-1.5) + assert_equal(absfunc(x), 1.5) + x = tp(0.0) + res = absfunc(x) + # assert_equal() checks zero signedness + assert_equal(res, 0.0) + x = tp(-0.0) + res = absfunc(x) + assert_equal(res, 0.0) + + def test_builtin_abs(self): + self._test_abs_func(abs) + + def test_numpy_abs(self): + self._test_abs_func(np.abs) + + if __name__ == "__main__": run_module_suite() |