Brian Bloniarz created MATH-1021:
------------------------------------
Summary: HypergeometricDistribution.sample suffers from integer
overflow
Key: MATH-1021
URL: https://issues.apache.org/jira/browse/MATH-1021
Project: Commons Math
Issue Type: Bug
Reporter: Brian Bloniarz
Hi, I have an application which broke when ported from commons math 2.2 to 3.2.
It looks like the HypergeometricDistribution.sample() method doesn't work as
well as it used to with large integer values -- the example code below should
return a sample between 0 and 50, but usually returns -50.
{code}
import org.apache.commons.math3.distribution.HypergeometricDistribution;
public class Foo {
public static void main(String[] args) {
HypergeometricDistribution a = new HypergeometricDistribution(
43130568, 42976365, 50);
System.out.printf("%d %d%n", a.getSupportLowerBound(),
a.getSupportUpperBound()); // Prints "0 50"
System.out.printf("%d%n",a.sample());
// Prints "-50"
}
}
{code}
In the debugger, I traced it as far as an integer overflow in
HypergeometricDistribution.getNumericalMean() -- instead of doing
{code}
return (double) (getSampleSize() * getNumberOfSuccesses()) / (double)
getPopulationSize();
{code}
it could do:
{code}
return getSampleSize() * ((double) getNumberOfSuccesses() / (double)
getPopulationSize());
{code}
This seemed to fix it, based on a quick test.
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira