You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2005/09/04 01:28:54 UTC

svn commit: r267515 - in /jakarta/commons/proper/math/branches/MATH_1_1/src: java/org/apache/commons/math/random/EmpiricalDistributionImpl.java test/org/apache/commons/math/random/EmpiricalDistributionTest.java

Author: psteitz
Date: Sat Sep  3 16:28:50 2005
New Revision: 267515

URL: http://svn.apache.org/viewcvs?rev=267515&view=rev
Log:
Fixed bin index overflow problem.
PR # BZ 36450
Submitted by: Keith McDonald

Modified:
    jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/random/EmpiricalDistributionImpl.java
    jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/random/EmpiricalDistributionTest.java

Modified: jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/random/EmpiricalDistributionImpl.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/random/EmpiricalDistributionImpl.java?rev=267515&r1=267514&r2=267515&view=diff
==============================================================================
--- jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/random/EmpiricalDistributionImpl.java (original)
+++ jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/random/EmpiricalDistributionImpl.java Sat Sep  3 16:28:50 2005
@@ -250,8 +250,7 @@
             while ((str = inputStream.readLine()) != null) {
                 val = Double.parseDouble(str);
                 SummaryStatistics stats =
-                    (SummaryStatistics) binStats.get(
-                        Math.max((int) Math.ceil((val - min) / delta) - 1, 0));
+                    (SummaryStatistics) binStats.get(findBin(min, val, delta));
                 stats.addValue(val);
             }
 
@@ -316,8 +315,7 @@
             for (int i = 0; i < inputArray.length; i++) {
                 SummaryStatistics stats =
                     (SummaryStatistics) binStats.get(
-                        Math.max((int) Math.ceil(
-                                (inputArray[i] - min) / delta)- 1, 0));
+                            findBin(min, inputArray[i], delta));
                 stats.addValue(inputArray[i]);
             }
         }
@@ -375,6 +373,20 @@
         }
         upperBounds[binCount-1] = 1.0d;
     }
+    
+    /**
+     * Returns the index of the bin to which the given value belongs
+     * 
+     * @param min  the minimum value
+     * @param value  the value whose bin we are trying to find
+     * @param delta  the grid size
+     * @return
+     */
+    private int findBin(double min, double value, double delta) {
+        return Math.min(
+                Math.max((int) Math.ceil((value- min) / delta) - 1, 0), 
+                binCount - 1);
+        }
 
     /**
      * Generates a random value from this distribution.

Modified: jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/random/EmpiricalDistributionTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/random/EmpiricalDistributionTest.java?rev=267515&r1=267514&r2=267515&view=diff
==============================================================================
--- jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/random/EmpiricalDistributionTest.java (original)
+++ jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/random/EmpiricalDistributionTest.java Sat Sep  3 16:28:50 2005
@@ -160,6 +160,14 @@
         tstDoubleGen(5);           
     }
     
+    /**
+     * Test bin index overflow problem (BZ 36450)
+     */
+    public void testBinIndexOverflow() throws Exception {
+        double[] x = new double[] {9474.94326071674, 2080107.8865462579};
+        new EmpiricalDistributionImpl().load(x);
+    }
+    
     public void testSerialization() {
         // Empty
         EmpiricalDistribution dist = new EmpiricalDistributionImpl();



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org