Author: erans Date: Sat Aug 10 00:57:48 2013 New Revision: 1512546 URL: http://svn.apache.org/r1512546 Log: MATH-1021 Reordering can prevent some overflow occurrences (fix suggested by Brian Bloniarz). Added unit test.
Modified: commons/proper/math/trunk/pom.xml commons/proper/math/trunk/src/changes/changes.xml commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/HypergeometricDistributionTest.java Modified: commons/proper/math/trunk/pom.xml URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/pom.xml?rev=1512546&r1=1512545&r2=1512546&view=diff ============================================================================== --- commons/proper/math/trunk/pom.xml (original) +++ commons/proper/math/trunk/pom.xml Sat Aug 10 00:57:48 2013 @@ -151,6 +151,9 @@ <name>Michael Bjorkegren</name> </contributor> <contributor> + <name>Brian Bloniarz</name> + </contributor> + <contributor> <name>John Bollinger</name> </contributor> <contributor> Modified: commons/proper/math/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1512546&r1=1512545&r2=1512546&view=diff ============================================================================== --- commons/proper/math/trunk/src/changes/changes.xml (original) +++ commons/proper/math/trunk/src/changes/changes.xml Sat Aug 10 00:57:48 2013 @@ -51,6 +51,9 @@ If the output is not quite correct, chec </properties> <body> <release version="x.y" date="TBD" description="TBD"> + <action dev="erans" type="fix" issue="MATH-1021" due-to="Brian Bloniarz"> + Fixed overflow in "HypergeometricDistribution". + </action> <action dev="erans" type="fix" issue="MATH-1020"> Fixed "nextPermutation" method (in "o.a.c.m.random.RandomDataGenerator"). This bug does not affect applications using a previous version of Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java?rev=1512546&r1=1512545&r2=1512546&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java Sat Aug 10 00:57:48 2013 @@ -265,7 +265,7 @@ public class HypergeometricDistribution * size {@code n}, the mean is {@code n * m / N}. */ public double getNumericalMean() { - return (double) (getSampleSize() * getNumberOfSuccesses()) / (double) getPopulationSize(); + return getSampleSize() * (getNumberOfSuccesses() / (double) getPopulationSize()); } /** Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/HypergeometricDistributionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/HypergeometricDistributionTest.java?rev=1512546&r1=1512545&r2=1512546&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/HypergeometricDistributionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/HypergeometricDistributionTest.java Sat Aug 10 00:57:48 2013 @@ -284,4 +284,18 @@ public class HypergeometricDistributionT double upper = 1.0 - dist.cumulativeProbability(k) + dist.probability(k); Assert.assertTrue(Precision.compareTo(1.0, upper, 1) == 0); } + + @Test + public void testMath1021() { + final int N = 43130568; + final int m = 42976365; + final int n = 50; + final HypergeometricDistribution dist = new HypergeometricDistribution(N, m, n); + + for (int i = 0; i < 100; i++) { + final int sample = dist.sample(); + Assert.assertTrue("sample=" + sample, 0 <= sample); + Assert.assertTrue("sample=" + sample, sample <= n); + } + } }