commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Brian Bloniarz (JIRA)" <>
Subject [jira] [Created] (MATH-1021) HypergeometricDistribution.sample suffers from integer overflow
Date Sat, 10 Aug 2013 00:00:48 GMT
Brian Bloniarz created MATH-1021:

             Summary: HypergeometricDistribution.sample suffers from integer overflow
                 Key: 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.

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

In the debugger, I traced it as far as an integer overflow in HypergeometricDistribution.getNumericalMean()
-- instead of doing
return (double) (getSampleSize() * getNumberOfSuccesses()) / (double) getPopulationSize();
it could do:
return getSampleSize() * ((double) getNumberOfSuccesses() / (double) getPopulationSize());
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:

View raw message