commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject 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
Date Sat, 03 Sep 2005 23:28:54 GMT
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


Mime
View raw message