From f1467e45ab538789090f968f549f88bd0b8c26c7 Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Sat, 10 Aug 2013 00:57:48 +0000 Subject: [PATCH] MATH-1021 Reordering can prevent some overflow occurrences (fix suggested by Brian Bloniarz). Added unit test. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1512546 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 3 +++ src/changes/changes.xml | 3 +++ .../distribution/HypergeometricDistribution.java | 2 +- .../HypergeometricDistributionTest.java | 14 ++++++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index b26a13d34..8969cd5a6 100644 --- a/pom.xml +++ b/pom.xml @@ -150,6 +150,9 @@ Michael Bjorkegren + + Brian Bloniarz + John Bollinger diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 1bfbedb30..20ab55107 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -51,6 +51,9 @@ If the output is not quite correct, check for invisible trailing spaces! + + Fixed overflow in "HypergeometricDistribution". + Fixed "nextPermutation" method (in "o.a.c.m.random.RandomDataGenerator"). This bug does not affect applications using a previous version of diff --git a/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java b/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java index 27691272f..81e180cdc 100644 --- a/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java +++ b/src/main/java/org/apache/commons/math3/distribution/HypergeometricDistribution.java @@ -265,7 +265,7 @@ public class HypergeometricDistribution extends AbstractIntegerDistribution { * size {@code n}, the mean is {@code n * m / N}. */ public double getNumericalMean() { - return (double) (getSampleSize() * getNumberOfSuccesses()) / (double) getPopulationSize(); + return getSampleSize() * (getNumberOfSuccesses() / (double) getPopulationSize()); } /** diff --git a/src/test/java/org/apache/commons/math3/distribution/HypergeometricDistributionTest.java b/src/test/java/org/apache/commons/math3/distribution/HypergeometricDistributionTest.java index 5598cd064..0893c3557 100644 --- a/src/test/java/org/apache/commons/math3/distribution/HypergeometricDistributionTest.java +++ b/src/test/java/org/apache/commons/math3/distribution/HypergeometricDistributionTest.java @@ -284,4 +284,18 @@ public class HypergeometricDistributionTest extends IntegerDistributionAbstractT 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); + } + } }