diff options
author | Julian Taylor <jtaylor.debian@googlemail.com> | 2014-02-18 19:08:03 +0100 |
---|---|---|
committer | Julian Taylor <jtaylor.debian@googlemail.com> | 2014-02-18 19:12:06 +0100 |
commit | 871c73b181706937ef7e14aecb24b23f65ed0993 (patch) | |
tree | 31bd88b3a81ad3823303bb166b281307f97c66c2 | |
parent | b5b47b144c78351f49b46731ccdb0de6ddf1276d (diff) | |
download | numpy-871c73b181706937ef7e14aecb24b23f65ed0993.tar.gz |
BUG: fix initialized half sum
pairwise summation added in 1.9 broke half sums if the first element is
initialized non-zero. E.g. d += x
-rw-r--r-- | numpy/core/src/umath/loops.c.src | 11 | ||||
-rw-r--r-- | numpy/core/tests/test_ufunc.py | 8 |
2 files changed, 13 insertions, 6 deletions
diff --git a/numpy/core/src/umath/loops.c.src b/numpy/core/src/umath/loops.c.src index a0edbc6dc..d6921efee 100644 --- a/numpy/core/src/umath/loops.c.src +++ b/numpy/core/src/umath/loops.c.src @@ -1791,20 +1791,19 @@ NPY_NO_EXPORT void HALF_@kind@(char **args, npy_intp *dimensions, npy_intp *steps, void *NPY_UNUSED(func)) { if (IS_BINARY_REDUCE) { + char *iop1 = args[0]; + float io1 = npy_half_to_float(*(npy_half *)iop1); #if @PW@ - npy_half * iop1 = (npy_half *)args[0]; npy_intp n = dimensions[0]; - *iop1 @OP@= npy_float_to_half(pairwise_sum_HALF((npy_half *)args[1], n, - steps[1] / (npy_intp)sizeof(npy_half))); + io1 @OP@= pairwise_sum_HALF((npy_half *)args[1], n, + steps[1] / (npy_intp)sizeof(npy_half)); #else - char *iop1 = args[0]; - float io1 = npy_half_to_float(*(npy_half *)iop1); BINARY_REDUCE_LOOP_INNER { io1 @OP@= npy_half_to_float(*(npy_half *)ip2); } - *((npy_half *)iop1) = npy_float_to_half(io1); #endif + *((npy_half *)iop1) = npy_float_to_half(io1); } else { BINARY_LOOP { diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py index b217936f7..f5a98431c 100644 --- a/numpy/core/tests/test_ufunc.py +++ b/numpy/core/tests/test_ufunc.py @@ -340,6 +340,10 @@ class TestUfunc(TestCase): assert_almost_equal(np.sum(d[-1::-2]), 250.) assert_almost_equal(np.sum(d[::-3]), 167.) assert_almost_equal(np.sum(d[-1::-3]), 167.) + # sum with first reduction entry != 0 + d = np.ones((1,), dtype=dt) + d += d + assert_almost_equal(d, 2.) def test_sum_complex(self): for dt in (np.complex64, np.complex128, np.clongdouble): @@ -361,6 +365,10 @@ class TestUfunc(TestCase): assert_almost_equal(np.sum(d[-1::-2]), 250. + 250j) assert_almost_equal(np.sum(d[::-3]), 167. + 167j) assert_almost_equal(np.sum(d[-1::-3]), 167. + 167j) + # sum with first reduction entry != 0 + d = np.ones((1,), dtype=dt) + 1j + d += d + assert_almost_equal(d, 2. + 2j) def test_inner1d(self): a = np.arange(6).reshape((2, 3)) |