diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2017-10-06 09:03:32 -0600 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2017-10-06 09:03:32 -0600 |
commit | 45e00937723064f14752fc88c18e7146e62789a0 (patch) | |
tree | 4aaf30fb025486fc2804f30e9e48f5320dfece5e | |
parent | e6932a8266ca2e1d1fdc7ec78e9254b6563b0e39 (diff) | |
download | python-numpy-45e00937723064f14752fc88c18e7146e62789a0.tar.gz python-numpy-45e00937723064f14752fc88c18e7146e62789a0.tar.bz2 python-numpy-45e00937723064f14752fc88c18e7146e62789a0.zip |
BUG: Check for NaN parameter in random.zipf.
Avoids infinite loop.
-rw-r--r-- | numpy/random/mtrand/mtrand.pyx | 10 | ||||
-rw-r--r-- | numpy/random/tests/test_random.py | 8 |
2 files changed, 12 insertions, 6 deletions
diff --git a/numpy/random/mtrand/mtrand.pyx b/numpy/random/mtrand/mtrand.pyx index 9e8a79804..adf820f0d 100644 --- a/numpy/random/mtrand/mtrand.pyx +++ b/numpy/random/mtrand/mtrand.pyx @@ -4076,13 +4076,15 @@ cdef class RandomState: if oa.shape == (): fa = PyFloat_AsDouble(a) - if fa <= 1.0: - raise ValueError("a <= 1.0") + # use logic that ensures NaN is rejected. + if not fa > 1.0: + raise ValueError("'a' must be a valid float > 1.0") return discd_array_sc(self.internal_state, rk_zipf, size, fa, self.lock) - if np.any(np.less_equal(oa, 1.0)): - raise ValueError("a <= 1.0") + # use logic that ensures NaN is rejected. + if not np.all(np.greater(oa, 1.0)): + raise ValueError("'a' must contain valid floats > 1.0") return discd_array(self.internal_state, rk_zipf, size, oa, self.lock) def geometric(self, p, size=None): diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index a530b9e13..e9c9bc492 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -1106,13 +1106,13 @@ class TestBroadcast(object): assert_raises(ValueError, nonc_f, bad_dfnum, dfden, nonc * 3) assert_raises(ValueError, nonc_f, dfnum, bad_dfden, nonc * 3) assert_raises(ValueError, nonc_f, dfnum, dfden, bad_nonc * 3) - + def test_noncentral_f_small_df(self): self.setSeed() desired = np.array([6.869638627492048, 0.785880199263955]) actual = np.random.noncentral_f(0.9, 0.9, 2, size=2) assert_array_almost_equal(actual, desired, decimal=14) - + def test_chisquare(self): df = [1] bad_df = [-1] @@ -1434,6 +1434,10 @@ class TestBroadcast(object): actual = zipf(a * 3) assert_array_equal(actual, desired) assert_raises(ValueError, zipf, bad_a * 3) + with np.errstate(invalid='ignore'): + assert_raises(ValueError, zipf, np.nan) + assert_raises(ValueError, zipf, [0, 0, np.nan]) + def test_geometric(self): p = [0.5] |