diff options
author | Jaime <jaime.frio@gmail.com> | 2015-03-08 12:57:39 -0700 |
---|---|---|
committer | Jaime <jaime.frio@gmail.com> | 2015-03-08 12:57:39 -0700 |
commit | 9fc077332b34a7b2a90126f5ae6cc50d57162db5 (patch) | |
tree | 5b79702dd18c18b3f31e4cf4a02d13b000fb83d8 | |
parent | 7f33cd972f96b0d1b660030adaf490ac275719ee (diff) | |
parent | 504d90ee39ad74093ced829cde1007eeea1e261d (diff) | |
download | numpy-9fc077332b34a7b2a90126f5ae6cc50d57162db5.tar.gz |
Merge pull request #5623 from rev112/fix_overflow_hypergeometric
BUG: Fix potential overflows in rk_hypergeometric_hrua()
-rw-r--r-- | numpy/random/mtrand/distributions.c | 4 | ||||
-rw-r--r-- | numpy/random/tests/test_regression.py | 11 |
2 files changed, 13 insertions, 2 deletions
diff --git a/numpy/random/mtrand/distributions.c b/numpy/random/mtrand/distributions.c index 001e2f6a1..6d9a23ced 100644 --- a/numpy/random/mtrand/distributions.c +++ b/numpy/random/mtrand/distributions.c @@ -788,9 +788,9 @@ long rk_hypergeometric_hrua(rk_state *state, long good, long bad, long sample) d4 = ((double)mingoodbad) / popsize; d5 = 1.0 - d4; d6 = m*d4 + 0.5; - d7 = sqrt((popsize - m) * sample * d4 *d5 / (popsize-1) + 0.5); + d7 = sqrt((double)(popsize - m) * sample * d4 * d5 / (popsize - 1) + 0.5); d8 = D1*d7 + D2; - d9 = (long)floor((double)((m+1)*(mingoodbad+1))/(popsize+2)); + d9 = (long)floor((double)(m + 1) * (mingoodbad + 1) / (popsize + 2)); d10 = (loggam(d9+1) + loggam(mingoodbad-d9+1) + loggam(m-d9+1) + loggam(maxgoodbad-m+d9+1)); d11 = min(min(m, mingoodbad)+1.0, floor(d6+16*d7)); diff --git a/numpy/random/tests/test_regression.py b/numpy/random/tests/test_regression.py index ccffd033e..1a5854e82 100644 --- a/numpy/random/tests/test_regression.py +++ b/numpy/random/tests/test_regression.py @@ -1,5 +1,6 @@ from __future__ import division, absolute_import, print_function +import sys from numpy.testing import (TestCase, run_module_suite, assert_, assert_array_equal) from numpy import random @@ -21,6 +22,16 @@ class TestRegression(TestCase): assert_(np.all(np.random.hypergeometric(3, 18, 11, size=10) < 4)) assert_(np.all(np.random.hypergeometric(18, 3, 11, size=10) > 0)) + # Test for ticket #5623 + args = [ + (2**20 - 2, 2**20 - 2, 2**20 - 2), # Check for 32-bit systems + ] + is_64bits = sys.maxsize > 2**32 + if is_64bits: + args.append((2**40 - 2, 2**40 - 2, 2**40 - 2)) # Check for 64-bit systems + for arg in args: + assert_(np.random.hypergeometric(*arg) > 0) + def test_logseries_convergence(self): # Test for ticket #923 N = 1000 |