diff options
author | Julian Taylor <jtaylor.debian@googlemail.com> | 2013-09-12 12:04:36 +0200 |
---|---|---|
committer | Julian Taylor <jtaylor.debian@googlemail.com> | 2013-09-12 12:19:07 +0200 |
commit | 1dd3778b8ea42d4c3e5315a80dd2baed36212649 (patch) | |
tree | 21186c8d91f70213583f23199fc886162714a9b5 | |
parent | 5d2e8a0968592eb4ff6dddeb9a92326da205d779 (diff) | |
download | numpy-1dd3778b8ea42d4c3e5315a80dd2baed36212649.tar.gz |
BUG: ensure raising of invalid on bad complex powers
put inf - inf into a volatile variable so it is not optimized away by
compilers.
-rw-r--r-- | numpy/core/src/umath/funcs.inc.src | 5 | ||||
-rw-r--r-- | numpy/core/tests/test_umath.py | 11 |
2 files changed, 9 insertions, 7 deletions
diff --git a/numpy/core/src/umath/funcs.inc.src b/numpy/core/src/umath/funcs.inc.src index 6d46efc44..9df39e41f 100644 --- a/numpy/core/src/umath/funcs.inc.src +++ b/numpy/core/src/umath/funcs.inc.src @@ -351,6 +351,7 @@ nc_pow@c@(@ctype@ *a, @ctype@ *b, @ctype@ *r) *r = npy_cpack@c@(0., 0.); } else { + volatile @ftype@ tmp = NPY_INFINITY; /* NB: there are four complex zeros; c0 = (+-0, +-0), so that unlike * for reals, c0**p, with `p` negative is in general * ill-defined. @@ -360,8 +361,8 @@ nc_pow@c@(@ctype@ *a, @ctype@ *b, @ctype@ *r) *r = npy_cpack@c@(NPY_NAN, NPY_NAN); /* Raise invalid */ - ar = NPY_INFINITY; - ar = ar - ar; + tmp -= NPY_INFINITY; + ar = tmp; } return; } diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index 3706cfa81..61f084a01 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -161,12 +161,13 @@ class TestPower(TestCase): # zero power assert_complex_equal(np.power(zero, 0), one) - assert_complex_equal(np.power(zero, 0+1j), cnan) + with np.errstate(invalid="ignore"): + assert_complex_equal(np.power(zero, 0+1j), cnan) - # negative power - for p in [0.33, 0.5, 1, 1.5, 2, 3, 4, 5, 6.6]: - assert_complex_equal(np.power(zero, -p), cnan) - assert_complex_equal(np.power(zero, -1+0.2j), cnan) + # negative power + for p in [0.33, 0.5, 1, 1.5, 2, 3, 4, 5, 6.6]: + assert_complex_equal(np.power(zero, -p), cnan) + assert_complex_equal(np.power(zero, -1+0.2j), cnan) def test_fast_power(self): x = np.array([1, 2, 3], np.int16) |