commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1181282 [1/2] - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/analysis/solvers/ main/java/org/apache/commons/math/complex/ main/java/org/apache/commons/math/ode/ main/java/org/apache/commons/math/ode/sampling/ main/j...
Date Mon, 10 Oct 2011 22:35:55 GMT
Author: erans
Date: Mon Oct 10 22:35:54 2011
New Revision: 1181282

URL: http://svn.apache.org/viewvc?rev=1181282&view=rev
Log:
MATH-689
Moved "equals..." and "compareTo" methods from "MathUtils" over to a new
"Precision" class.

Added:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Precision.java   (with props)
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/PrecisionTest.java   (with props)
Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BracketingNthOrderBrentSolver.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BrentSolver.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/ode/AbstractIntegrator.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/ode/sampling/StepNormalizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexSolver.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexTableau.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/BrentOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/AbstractStorelessUnivariateStatistic.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/MultivariateSummaryStatistics.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/StatisticalSummaryValues.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/SummaryStatistics.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtilsTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ExponentialDistributionTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/filter/KalmanFilterTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/linear/SimplexSolverTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/StatUtilsTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/AggregateSummaryStatisticsTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/DescriptiveStatisticsTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/transform/FastHadamardTransformerTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/OpenIntToDoubleHashMapTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BracketingNthOrderBrentSolver.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BracketingNthOrderBrentSolver.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BracketingNthOrderBrentSolver.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BracketingNthOrderBrentSolver.java Mon Oct 10 22:35:54 2011
@@ -22,7 +22,7 @@ import org.apache.commons.math.exception
 import org.apache.commons.math.exception.NoBracketingException;
 import org.apache.commons.math.exception.NumberIsTooSmallException;
 import org.apache.commons.math.util.FastMath;
-import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 
 /**
  * This class implements a modification of the <a
@@ -152,14 +152,14 @@ public class BracketingNthOrderBrentSolv
 
         // evaluate initial guess
         y[1] = computeObjectiveValue(x[1]);
-        if (MathUtils.equals(y[1], 0.0, 1)) {
+        if (Precision.equals(y[1], 0.0, 1)) {
             // return the initial guess if it is a perfect root.
             return x[1];
         }
 
         // evaluate first  endpoint
         y[0] = computeObjectiveValue(x[0]);
-        if (MathUtils.equals(y[0], 0.0, 1)) {
+        if (Precision.equals(y[0], 0.0, 1)) {
             // return the first endpoint if it is a perfect root.
             return x[0];
         }
@@ -176,7 +176,7 @@ public class BracketingNthOrderBrentSolv
 
             // evaluate second endpoint
             y[2] = computeObjectiveValue(x[2]);
-            if (MathUtils.equals(y[2], 0.0, 1)) {
+            if (Precision.equals(y[2], 0.0, 1)) {
                 // return the second endpoint if it is a perfect root.
                 return x[2];
             }
@@ -281,7 +281,7 @@ public class BracketingNthOrderBrentSolv
 
             // evaluate the function at the guessed root
             final double nextY = computeObjectiveValue(nextX);
-            if (MathUtils.equals(nextY, 0.0, 1)) {
+            if (Precision.equals(nextY, 0.0, 1)) {
                 // we have found an exact root, since it is not an approximation
                 // we don't need to bother about the allowed solutions setting
                 return nextX;

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BrentSolver.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BrentSolver.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BrentSolver.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/solvers/BrentSolver.java Mon Oct 10 22:35:54 2011
@@ -19,7 +19,7 @@ package org.apache.commons.math.analysis
 
 import org.apache.commons.math.exception.NoBracketingException;
 import org.apache.commons.math.util.FastMath;
-import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 
 /**
  * This class implements the <a href="http://mathworld.wolfram.com/BrentsMethod.html">
@@ -162,7 +162,7 @@ public class BrentSolver extends Abstrac
             final double m = 0.5 * (c - b);
 
             if (FastMath.abs(m) <= tol ||
-                MathUtils.equals(fb, 0))  {
+                Precision.equals(fb, 0))  {
                 return b;
             }
             if (FastMath.abs(e) < tol ||

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java Mon Oct 10 22:35:54 2011
@@ -45,9 +45,10 @@ import org.apache.commons.math.util.Fast
  * Note that this is in contradiction with the IEEE-754 standard for floating
  * point numbers (according to which the test {@code x == x} must fail if
  * {@code x} is {@code NaN}). The method
- * {@link MathUtils#equals(double,double,int) equals for primitive double} in
- * {@link MathUtils} conforms with IEEE-754 while this class conforms with
- * the standard behavior for Java object types.
+ * {@link org.apache.commons.math.util.Precision#equals(double,double,int)
+ * equals for primitive double} in {@link org.apache.commons.math.util.Precision}
+ * conforms with IEEE-754 while this class conforms with the standard behavior
+ * for Java object types.
  * <br/>
  * Implements Serializable since 2.0
  *

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/ode/AbstractIntegrator.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/ode/AbstractIntegrator.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/ode/AbstractIntegrator.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/ode/AbstractIntegrator.java Mon Oct 10 22:35:54 2011
@@ -41,6 +41,7 @@ import org.apache.commons.math.ode.sampl
 import org.apache.commons.math.util.FastMath;
 import org.apache.commons.math.util.Incrementor;
 import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 
 /**
  * Base class managing common boilerplate for all integrators.
@@ -362,7 +363,7 @@ public abstract class AbstractIntegrator
                 state.stepAccepted(currentT, currentY);
                 isLastStep = isLastStep || state.stop();
             }
-            isLastStep = isLastStep || MathUtils.equals(currentT, tEnd, 1);
+            isLastStep = isLastStep || Precision.equals(currentT, tEnd, 1);
 
             // handle the remaining part of the step, after all events if any
             for (StepHandler handler : stepHandlers) {

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/ode/sampling/StepNormalizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/ode/sampling/StepNormalizer.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/ode/sampling/StepNormalizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/ode/sampling/StepNormalizer.java Mon Oct 10 22:35:54 2011
@@ -19,6 +19,7 @@ package org.apache.commons.math.ode.samp
 
 import org.apache.commons.math.util.FastMath;
 import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 
 /**
  * This class wraps an object implementing {@link FixedStepHandler}
@@ -212,7 +213,7 @@ public class StepNormalizer implements S
                           lastTime + h :
                           (FastMath.floor(lastTime / h) + 1) * h;
         if (mode == StepNormalizerMode.MULTIPLES &&
-            MathUtils.equals(nextTime, lastTime, 1)) {
+            Precision.equals(nextTime, lastTime, 1)) {
             nextTime += h;
         }
 

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexSolver.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexSolver.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexSolver.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexSolver.java Mon Oct 10 22:35:54 2011
@@ -22,7 +22,7 @@ import java.util.List;
 
 import org.apache.commons.math.exception.MaxCountExceededException;
 import org.apache.commons.math.optimization.RealPointValuePair;
-import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 
 
 /**
@@ -71,7 +71,7 @@ public class SimplexSolver extends Abstr
         Integer minPos = null;
         for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getWidth() - 1; i++) {
             final double entry = tableau.getEntry(0, i);
-            if (MathUtils.compareTo(entry, minValue, maxUlps) < 0) {
+            if (Precision.compareTo(entry, minValue, maxUlps) < 0) {
                 minValue = entry;
                 minPos = i;
             }
@@ -93,9 +93,9 @@ public class SimplexSolver extends Abstr
             final double rhs = tableau.getEntry(i, tableau.getWidth() - 1);
             final double entry = tableau.getEntry(i, col);
 
-            if (MathUtils.compareTo(entry, 0d, maxUlps) > 0) {
+            if (Precision.compareTo(entry, 0d, maxUlps) > 0) {
                 final double ratio = rhs / entry;
-                final int cmp = MathUtils.compareTo(ratio, minRatio, maxUlps);
+                final int cmp = Precision.compareTo(ratio, minRatio, maxUlps);
                 if (cmp == 0) {
                     minRatioPositions.add(i);
                 } else if (cmp < 0) {
@@ -115,7 +115,7 @@ public class SimplexSolver extends Abstr
             for (int i = 0; i < tableau.getNumArtificialVariables(); i++) {
               int column = i + tableau.getArtificialVariableOffset();
               final double entry = tableau.getEntry(row, column);
-              if (MathUtils.equals(entry, 1d, maxUlps) &&
+              if (Precision.equals(entry, 1d, maxUlps) &&
                   row.equals(tableau.getBasicRow(column))) {
                 return row;
               }
@@ -175,7 +175,7 @@ public class SimplexSolver extends Abstr
         }
 
         // if W is not zero then we have no feasible solution
-        if (!MathUtils.equals(tableau.getEntry(0, tableau.getRhsOffset()), 0d, epsilon)) {
+        if (!Precision.equals(tableau.getEntry(0, tableau.getRhsOffset()), 0d, epsilon)) {
             throw new NoFeasibleSolutionException();
         }
     }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexTableau.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexTableau.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexTableau.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/linear/SimplexTableau.java Mon Oct 10 22:35:54 2011
@@ -33,7 +33,7 @@ import org.apache.commons.math.linear.Re
 import org.apache.commons.math.linear.RealVector;
 import org.apache.commons.math.optimization.GoalType;
 import org.apache.commons.math.optimization.RealPointValuePair;
-import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 
 /**
  * A tableau for use in the Simplex method.
@@ -311,9 +311,9 @@ class SimplexTableau implements Serializ
         Integer row = null;
         for (int i = 0; i < getHeight(); i++) {
             final double entry = getEntry(i, col);
-            if (MathUtils.equals(entry, 1d, maxUlps) && (row == null)) {
+            if (Precision.equals(entry, 1d, maxUlps) && (row == null)) {
                 row = i;
-            } else if (!MathUtils.equals(entry, 0d, maxUlps)) {
+            } else if (!Precision.equals(entry, 0d, maxUlps)) {
                 return null;
             }
         }
@@ -335,7 +335,7 @@ class SimplexTableau implements Serializ
         // positive cost non-artificial variables
         for (int i = getNumObjectiveFunctions(); i < getArtificialVariableOffset(); i++) {
             final double entry = tableau.getEntry(0, i);
-            if (MathUtils.compareTo(entry, 0d, maxUlps) > 0) {
+            if (Precision.compareTo(entry, 0d, maxUlps) > 0) {
                 columnsToDrop.add(i);
             }
         }
@@ -381,7 +381,7 @@ class SimplexTableau implements Serializ
     boolean isOptimal() {
         for (int i = getNumObjectiveFunctions(); i < getWidth() - 1; i++) {
             final double entry = tableau.getEntry(0, i);
-            if (MathUtils.compareTo(entry, 0d, epsilon) < 0) {
+            if (Precision.compareTo(entry, 0d, epsilon) < 0) {
                 return false;
             }
         }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/BrentOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/BrentOptimizer.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/BrentOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/univariate/BrentOptimizer.java Mon Oct 10 22:35:54 2011
@@ -16,7 +16,7 @@
  */
 package org.apache.commons.math.optimization.univariate;
 
-import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 import org.apache.commons.math.util.FastMath;
 import org.apache.commons.math.exception.NumberIsTooSmallException;
 import org.apache.commons.math.exception.NotStrictlyPositiveException;
@@ -220,14 +220,14 @@ public class BrentOptimizer extends Abst
                         b = u;
                     }
                     if (fu <= fw ||
-                        MathUtils.equals(w, x)) {
+                        Precision.equals(w, x)) {
                         v = w;
                         fv = fw;
                         w = u;
                         fw = fu;
                     } else if (fu <= fv ||
-                               MathUtils.equals(v, x) ||
-                               MathUtils.equals(v, w)) {
+                               Precision.equals(v, x) ||
+                               Precision.equals(v, w)) {
                         v = u;
                         fv = fu;
                     }

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/AbstractStorelessUnivariateStatistic.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/AbstractStorelessUnivariateStatistic.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/AbstractStorelessUnivariateStatistic.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/AbstractStorelessUnivariateStatistic.java Mon Oct 10 22:35:54 2011
@@ -19,6 +19,7 @@ package org.apache.commons.math.stat.des
 import org.apache.commons.math.exception.util.LocalizedFormats;
 import org.apache.commons.math.exception.NullArgumentException;
 import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 
 /**
  *
@@ -166,8 +167,8 @@ public abstract class AbstractStorelessU
             return false;
         }
         AbstractStorelessUnivariateStatistic stat = (AbstractStorelessUnivariateStatistic) object;
-        return MathUtils.equalsIncludingNaN(stat.getResult(), this.getResult()) &&
-               MathUtils.equalsIncludingNaN(stat.getN(), this.getN());
+        return Precision.equalsIncludingNaN(stat.getResult(), this.getResult()) &&
+               Precision.equalsIncludingNaN(stat.getN(), this.getN());
     }
 
     /**

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/MultivariateSummaryStatistics.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/MultivariateSummaryStatistics.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/MultivariateSummaryStatistics.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/MultivariateSummaryStatistics.java Mon Oct 10 22:35:54 2011
@@ -32,6 +32,7 @@ import org.apache.commons.math.stat.desc
 import org.apache.commons.math.stat.descriptive.summary.SumOfLogs;
 import org.apache.commons.math.stat.descriptive.summary.SumOfSquares;
 import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 import org.apache.commons.math.util.FastMath;
 
 /**
@@ -376,7 +377,7 @@ public class MultivariateSummaryStatisti
                MathUtils.equalsIncludingNaN(stat.getMax(),           getMax())           &&
                MathUtils.equalsIncludingNaN(stat.getMean(),          getMean())          &&
                MathUtils.equalsIncludingNaN(stat.getMin(),           getMin())           &&
-               MathUtils.equalsIncludingNaN(stat.getN(),             getN())             &&
+               Precision.equalsIncludingNaN(stat.getN(),             getN())             &&
                MathUtils.equalsIncludingNaN(stat.getSum(),           getSum())           &&
                MathUtils.equalsIncludingNaN(stat.getSumSq(),         getSumSq())         &&
                MathUtils.equalsIncludingNaN(stat.getSumLog(),        getSumLog())        &&

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/StatisticalSummaryValues.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/StatisticalSummaryValues.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/StatisticalSummaryValues.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/StatisticalSummaryValues.java Mon Oct 10 22:35:54 2011
@@ -20,6 +20,7 @@ import java.io.Serializable;
 
 import org.apache.commons.math.util.FastMath;
 import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 
 /**
  *  Value object representing the results of a univariate statistical summary.
@@ -137,12 +138,12 @@ public class StatisticalSummaryValues im
             return false;
         }
         StatisticalSummaryValues stat = (StatisticalSummaryValues) object;
-        return MathUtils.equalsIncludingNaN(stat.getMax(),      getMax())  &&
-               MathUtils.equalsIncludingNaN(stat.getMean(),     getMean()) &&
-               MathUtils.equalsIncludingNaN(stat.getMin(),      getMin())  &&
-               MathUtils.equalsIncludingNaN(stat.getN(),        getN())    &&
-               MathUtils.equalsIncludingNaN(stat.getSum(),      getSum())  &&
-               MathUtils.equalsIncludingNaN(stat.getVariance(), getVariance());
+        return Precision.equalsIncludingNaN(stat.getMax(),      getMax())  &&
+               Precision.equalsIncludingNaN(stat.getMean(),     getMean()) &&
+               Precision.equalsIncludingNaN(stat.getMin(),      getMin())  &&
+               Precision.equalsIncludingNaN(stat.getN(),        getN())    &&
+               Precision.equalsIncludingNaN(stat.getSum(),      getSum())  &&
+               Precision.equalsIncludingNaN(stat.getVariance(), getVariance());
     }
 
     /**

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/SummaryStatistics.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/SummaryStatistics.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/SummaryStatistics.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/SummaryStatistics.java Mon Oct 10 22:35:54 2011
@@ -31,6 +31,7 @@ import org.apache.commons.math.stat.desc
 import org.apache.commons.math.stat.descriptive.summary.SumOfLogs;
 import org.apache.commons.math.stat.descriptive.summary.SumOfSquares;
 import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 import org.apache.commons.math.util.FastMath;
 
 /**
@@ -363,14 +364,14 @@ public class SummaryStatistics implement
             return false;
         }
         SummaryStatistics stat = (SummaryStatistics)object;
-        return MathUtils.equalsIncludingNaN(stat.getGeometricMean(), getGeometricMean()) &&
-               MathUtils.equalsIncludingNaN(stat.getMax(),           getMax())           &&
-               MathUtils.equalsIncludingNaN(stat.getMean(),          getMean())          &&
-               MathUtils.equalsIncludingNaN(stat.getMin(),           getMin())           &&
-               MathUtils.equalsIncludingNaN(stat.getN(),             getN())             &&
-               MathUtils.equalsIncludingNaN(stat.getSum(),           getSum())           &&
-               MathUtils.equalsIncludingNaN(stat.getSumsq(),         getSumsq())         &&
-               MathUtils.equalsIncludingNaN(stat.getVariance(),      getVariance());
+        return Precision.equalsIncludingNaN(stat.getGeometricMean(), getGeometricMean()) &&
+               Precision.equalsIncludingNaN(stat.getMax(),           getMax())           &&
+               Precision.equalsIncludingNaN(stat.getMean(),          getMean())          &&
+               Precision.equalsIncludingNaN(stat.getMin(),           getMin())           &&
+               Precision.equalsIncludingNaN(stat.getN(),             getN())             &&
+               Precision.equalsIncludingNaN(stat.getSum(),           getSum())           &&
+               Precision.equalsIncludingNaN(stat.getSumsq(),         getSumsq())         &&
+               Precision.equalsIncludingNaN(stat.getVariance(),      getVariance());
     }
 
     /**

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java Mon Oct 10 22:35:54 2011
@@ -76,12 +76,6 @@ public final class MathUtils {
     /** 0.0 cast as a short. */
     private static final short ZS = (short)0;
 
-    /** Offset to order signed double numbers lexicographically. */
-    private static final long SGN_MASK = 0x8000000000000000L;
-
-    /** Offset to order signed double numbers lexicographically. */
-    private static final int SGN_MASK_FLOAT = 0x80000000;
-
     /** All long-representable factorials */
     private static final long[] FACTORIALS = new long[] {
                        1l,                  1l,                   2l,
@@ -389,51 +383,6 @@ public final class MathUtils {
     }
 
     /**
-     * Compares two numbers given some amount of allowed error.
-     *
-     * @param x the first number
-     * @param y the second number
-     * @param eps the amount of error to allow when checking for equality
-     * @return <ul><li>0 if  {@link #equals(double, double, double) equals(x, y, eps)}</li>
-     *       <li>&lt; 0 if !{@link #equals(double, double, double) equals(x, y, eps)} &amp;&amp; x &lt; y</li>
-     *       <li>> 0 if !{@link #equals(double, double, double) equals(x, y, eps)} &amp;&amp; x > y</li></ul>
-     */
-    public static int compareTo(double x, double y, double eps) {
-        if (equals(x, y, eps)) {
-            return 0;
-        } else if (x < y) {
-            return -1;
-        }
-        return 1;
-    }
-
-    /**
-     * Compares two numbers given some amount of allowed error.
-     * Two float numbers are considered equal if there are {@code (maxUlps - 1)}
-     * (or fewer) floating point numbers between them, i.e. two adjacent floating
-     * point numbers are considered equal.
-     * Adapted from <a
-     * href="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm">
-     * Bruce Dawson</a>
-     *
-     * @param x first value
-     * @param y second value
-     * @param maxUlps {@code (maxUlps - 1)} is the number of floating point
-     * values between {@code x} and {@code y}.
-     * @return <ul><li>0 if  {@link #equals(double, double, int) equals(x, y, maxUlps)}</li>
-     *       <li>&lt; 0 if !{@link #equals(double, double, int) equals(x, y, maxUlps)} &amp;&amp; x &lt; y</li>
-     *       <li>> 0 if !{@link #equals(double, double, int) equals(x, y, maxUlps)} &amp;&amp; x > y</li></ul>
-     */
-    public static int compareTo(final double x, final double y, final int maxUlps) {
-        if (equals(x, y, maxUlps)) {
-            return 0;
-        } else if (x < y) {
-            return -1;
-        }
-        return 1;
-    }
-
-    /**
      * Returns the <a href="http://mathworld.wolfram.com/HyperbolicCosine.html">
      * hyperbolic cosine</a> of x.
      *
@@ -445,114 +394,9 @@ public final class MathUtils {
     }
 
     /**
-     * Returns true iff they are equal as defined by
-     * {@link #equals(float,float,int) equals(x, y, 1)}.
-     *
-     * @param x first value
-     * @param y second value
-     * @return {@code true} if the values are equal.
-     */
-    public static boolean equals(float x, float y) {
-        return equals(x, y, 1);
-    }
-
-    /**
-     * Returns true if both arguments are NaN or neither is NaN and they are
-     * equal as defined by {@link #equals(float,float) equals(x, y, 1)}.
-     *
-     * @param x first value
-     * @param y second value
-     * @return {@code true} if the values are equal or both are NaN.
-     * @since 2.2
-     */
-    public static boolean equalsIncludingNaN(float x, float y) {
-        return (Float.isNaN(x) && Float.isNaN(y)) || equals(x, y, 1);
-    }
-
-    /**
-     * Returns true if both arguments are equal or within the range of allowed
-     * error (inclusive).
-     *
-     * @param x first value
-     * @param y second value
-     * @param eps the amount of absolute error to allow.
-     * @return {@code true} if the values are equal or within range of each other.
-     * @since 2.2
-     */
-    public static boolean equals(float x, float y, float eps) {
-        return equals(x, y, 1) || FastMath.abs(y - x) <= eps;
-    }
-
-    /**
-     * Returns true if both arguments are NaN or are equal or within the range
-     * of allowed error (inclusive).
-     *
-     * @param x first value
-     * @param y second value
-     * @param eps the amount of absolute error to allow.
-     * @return {@code true} if the values are equal or within range of each other,
-     * or both are NaN.
-     * @since 2.2
-     */
-    public static boolean equalsIncludingNaN(float x, float y, float eps) {
-        return equalsIncludingNaN(x, y) || (FastMath.abs(y - x) <= eps);
-    }
-
-    /**
-     * Returns true if both arguments are equal or within the range of allowed
-     * error (inclusive).
-     * Two float numbers are considered equal if there are {@code (maxUlps - 1)}
-     * (or fewer) floating point numbers between them, i.e. two adjacent floating
-     * point numbers are considered equal.
-     * Adapted from <a
-     * href="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm">
-     * Bruce Dawson</a>
-     *
-     * @param x first value
-     * @param y second value
-     * @param maxUlps {@code (maxUlps - 1)} is the number of floating point
-     * values between {@code x} and {@code y}.
-     * @return {@code true} if there are fewer than {@code maxUlps} floating
-     * point values between {@code x} and {@code y}.
-     * @since 2.2
-     */
-    public static boolean equals(float x, float y, int maxUlps) {
-        int xInt = Float.floatToIntBits(x);
-        int yInt = Float.floatToIntBits(y);
-
-        // Make lexicographically ordered as a two's-complement integer.
-        if (xInt < 0) {
-            xInt = SGN_MASK_FLOAT - xInt;
-        }
-        if (yInt < 0) {
-            yInt = SGN_MASK_FLOAT - yInt;
-        }
-
-        final boolean isEqual = FastMath.abs(xInt - yInt) <= maxUlps;
-
-        return isEqual && !Float.isNaN(x) && !Float.isNaN(y);
-    }
-
-    /**
-     * Returns true if both arguments are NaN or if they are equal as defined
-     * by {@link #equals(float,float,int) equals(x, y, maxUlps)}.
-     *
-     * @param x first value
-     * @param y second value
-     * @param maxUlps {@code (maxUlps - 1)} is the number of floating point
-     * values between {@code x} and {@code y}.
-     * @return {@code true} if both arguments are NaN or if there are less than
-     * {@code maxUlps} floating point values between {@code x} and {@code y}.
-     * @since 2.2
-     */
-    public static boolean equalsIncludingNaN(float x, float y, int maxUlps) {
-        return (Float.isNaN(x) && Float.isNaN(y)) || equals(x, y, maxUlps);
-    }
-
-    /**
      * Returns true iff both arguments are null or have same dimensions and all
      * their elements are equal as defined by
-     * {@link #equals(float,float)}.
+     * {@link Precision#equals(float,float)}.
      *
      * @param x first array
      * @param y second array
@@ -567,7 +411,7 @@ public final class MathUtils {
             return false;
         }
         for (int i = 0; i < x.length; ++i) {
-            if (!equals(x[i], y[i])) {
+            if (!Precision.equals(x[i], y[i])) {
                 return false;
             }
         }
@@ -577,7 +421,7 @@ public final class MathUtils {
     /**
      * Returns true iff both arguments are null or have same dimensions and all
      * their elements are equal as defined by
-     * {@link #equalsIncludingNaN(double,double) this method}.
+     * {@link Precision#equalsIncludingNaN(double,double) this method}.
      *
      * @param x first array
      * @param y second array
@@ -593,7 +437,7 @@ public final class MathUtils {
             return false;
         }
         for (int i = 0; i < x.length; ++i) {
-            if (!equalsIncludingNaN(x[i], y[i])) {
+            if (!Precision.equalsIncludingNaN(x[i], y[i])) {
                 return false;
             }
         }
@@ -601,114 +445,9 @@ public final class MathUtils {
     }
 
     /**
-     * Returns true iff they are equal as defined by
-     * {@link #equals(double,double,int) equals(x, y, 1)}.
-     *
-     * @param x first value
-     * @param y second value
-     * @return {@code true} if the values are equal.
-     */
-    public static boolean equals(double x, double y) {
-        return equals(x, y, 1);
-    }
-
-    /**
-     * Returns true if both arguments are NaN or neither is NaN and they are
-     * equal as defined by {@link #equals(double,double) equals(x, y, 1)}.
-     *
-     * @param x first value
-     * @param y second value
-     * @return {@code true} if the values are equal or both are NaN.
-     * @since 2.2
-     */
-    public static boolean equalsIncludingNaN(double x, double y) {
-        return (Double.isNaN(x) && Double.isNaN(y)) || equals(x, y, 1);
-    }
-
-    /**
-     * Returns {@code true} if there is no double value strictly between the
-     * arguments or the difference between them is within the range of allowed
-     * error (inclusive).
-     *
-     * @param x First value.
-     * @param y Second value.
-     * @param eps Amount of allowed absolute error.
-     * @return {@code true} if the values are two adjacent floating point
-     * numbers or they are within range of each other.
-     */
-    public static boolean equals(double x, double y, double eps) {
-        return equals(x, y, 1) || FastMath.abs(y - x) <= eps;
-    }
-
-    /**
-     * Returns true if both arguments are NaN or are equal or within the range
-     * of allowed error (inclusive).
-     *
-     * @param x first value
-     * @param y second value
-     * @param eps the amount of absolute error to allow.
-     * @return {@code true} if the values are equal or within range of each other,
-     * or both are NaN.
-     * @since 2.2
-     */
-    public static boolean equalsIncludingNaN(double x, double y, double eps) {
-        return equalsIncludingNaN(x, y) || (FastMath.abs(y - x) <= eps);
-    }
-
-    /**
-     * Returns true if both arguments are equal or within the range of allowed
-     * error (inclusive).
-     * Two float numbers are considered equal if there are {@code (maxUlps - 1)}
-     * (or fewer) floating point numbers between them, i.e. two adjacent floating
-     * point numbers are considered equal.
-     * Adapted from <a
-     * href="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm">
-     * Bruce Dawson</a>
-     *
-     * @param x first value
-     * @param y second value
-     * @param maxUlps {@code (maxUlps - 1)} is the number of floating point
-     * values between {@code x} and {@code y}.
-     * @return {@code true} if there are fewer than {@code maxUlps} floating
-     * point values between {@code x} and {@code y}.
-     */
-    public static boolean equals(double x, double y, int maxUlps) {
-        long xInt = Double.doubleToLongBits(x);
-        long yInt = Double.doubleToLongBits(y);
-
-        // Make lexicographically ordered as a two's-complement integer.
-        if (xInt < 0) {
-            xInt = SGN_MASK - xInt;
-        }
-        if (yInt < 0) {
-            yInt = SGN_MASK - yInt;
-        }
-
-        final boolean isEqual = FastMath.abs(xInt - yInt) <= maxUlps;
-
-        return isEqual && !Double.isNaN(x) && !Double.isNaN(y);
-    }
-
-    /**
-     * Returns true if both arguments are NaN or if they are equal as defined
-     * by {@link #equals(double,double,int) equals(x, y, maxUlps)}.
-     *
-     * @param x first value
-     * @param y second value
-     * @param maxUlps {@code (maxUlps - 1)} is the number of floating point
-     * values between {@code x} and {@code y}.
-     * @return {@code true} if both arguments are NaN or if there are less than
-     * {@code maxUlps} floating point values between {@code x} and {@code y}.
-     * @since 2.2
-     */
-    public static boolean equalsIncludingNaN(double x, double y, int maxUlps) {
-        return (Double.isNaN(x) && Double.isNaN(y)) || equals(x, y, maxUlps);
-    }
-
-    /**
      * Returns {@code true} iff both arguments are {@code null} or have same
      * dimensions and all their elements are equal as defined by
-     * {@link #equals(double,double)}.
+     * {@link Precision#equals(double,double)}.
      *
      * @param x First array.
      * @param y Second array.
@@ -723,7 +462,7 @@ public final class MathUtils {
             return false;
         }
         for (int i = 0; i < x.length; ++i) {
-            if (!equals(x[i], y[i])) {
+            if (!Precision.equals(x[i], y[i])) {
                 return false;
             }
         }
@@ -733,7 +472,7 @@ public final class MathUtils {
     /**
      * Returns {@code true} iff both arguments are {@code null} or have same
      * dimensions and all their elements are equal as defined by
-     * {@link #equalsIncludingNaN(double,double) this method}.
+     * {@link Precision#equalsIncludingNaN(double,double) this method}.
      *
      * @param x First array.
      * @param y Second array.
@@ -749,7 +488,7 @@ public final class MathUtils {
             return false;
         }
         for (int i = 0; i < x.length; ++i) {
-            if (!equalsIncludingNaN(x[i], y[i])) {
+            if (!Precision.equalsIncludingNaN(x[i], y[i])) {
                 return false;
             }
         }

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Precision.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Precision.java?rev=1181282&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Precision.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Precision.java Mon Oct 10 22:35:54 2011
@@ -0,0 +1,292 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math.util;
+
+/**
+ * Utilities for comparing numbers.
+ *
+ * @since 3.0
+ * @version $Id$
+ */
+public class Precision {
+    /** Offset to order signed double numbers lexicographically. */
+    private static final long SGN_MASK = 0x8000000000000000L;
+
+    /** Offset to order signed double numbers lexicographically. */
+    private static final int SGN_MASK_FLOAT = 0x80000000;
+
+    /**
+     * Private Constructor
+     */
+    private Precision() {}
+
+    /**
+     * Compares two numbers given some amount of allowed error.
+     *
+     * @param x the first number
+     * @param y the second number
+     * @param eps the amount of error to allow when checking for equality
+     * @return <ul><li>0 if  {@link #equals(double, double, double) equals(x, y, eps)}</li>
+     *       <li>&lt; 0 if !{@link #equals(double, double, double) equals(x, y, eps)} &amp;&amp; x &lt; y</li>
+     *       <li>> 0 if !{@link #equals(double, double, double) equals(x, y, eps)} &amp;&amp; x > y</li></ul>
+     */
+    public static int compareTo(double x, double y, double eps) {
+        if (equals(x, y, eps)) {
+            return 0;
+        } else if (x < y) {
+            return -1;
+        }
+        return 1;
+    }
+
+    /**
+     * Compares two numbers given some amount of allowed error.
+     * Two float numbers are considered equal if there are {@code (maxUlps - 1)}
+     * (or fewer) floating point numbers between them, i.e. two adjacent floating
+     * point numbers are considered equal.
+     * Adapted from <a
+     * href="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm">
+     * Bruce Dawson</a>
+     *
+     * @param x first value
+     * @param y second value
+     * @param maxUlps {@code (maxUlps - 1)} is the number of floating point
+     * values between {@code x} and {@code y}.
+     * @return <ul><li>0 if  {@link #equals(double, double, int) equals(x, y, maxUlps)}</li>
+     *       <li>&lt; 0 if !{@link #equals(double, double, int) equals(x, y, maxUlps)} &amp;&amp; x &lt; y</li>
+     *       <li>> 0 if !{@link #equals(double, double, int) equals(x, y, maxUlps)} &amp;&amp; x > y</li></ul>
+     */
+    public static int compareTo(final double x, final double y, final int maxUlps) {
+        if (equals(x, y, maxUlps)) {
+            return 0;
+        } else if (x < y) {
+            return -1;
+        }
+        return 1;
+    }
+
+    /**
+     * Returns true iff they are equal as defined by
+     * {@link #equals(float,float,int) equals(x, y, 1)}.
+     *
+     * @param x first value
+     * @param y second value
+     * @return {@code true} if the values are equal.
+     */
+    public static boolean equals(float x, float y) {
+        return equals(x, y, 1);
+    }
+
+    /**
+     * Returns true if both arguments are NaN or neither is NaN and they are
+     * equal as defined by {@link #equals(float,float) equals(x, y, 1)}.
+     *
+     * @param x first value
+     * @param y second value
+     * @return {@code true} if the values are equal or both are NaN.
+     * @since 2.2
+     */
+    public static boolean equalsIncludingNaN(float x, float y) {
+        return (Float.isNaN(x) && Float.isNaN(y)) || equals(x, y, 1);
+    }
+
+    /**
+     * Returns true if both arguments are equal or within the range of allowed
+     * error (inclusive).
+     *
+     * @param x first value
+     * @param y second value
+     * @param eps the amount of absolute error to allow.
+     * @return {@code true} if the values are equal or within range of each other.
+     * @since 2.2
+     */
+    public static boolean equals(float x, float y, float eps) {
+        return equals(x, y, 1) || FastMath.abs(y - x) <= eps;
+    }
+
+    /**
+     * Returns true if both arguments are NaN or are equal or within the range
+     * of allowed error (inclusive).
+     *
+     * @param x first value
+     * @param y second value
+     * @param eps the amount of absolute error to allow.
+     * @return {@code true} if the values are equal or within range of each other,
+     * or both are NaN.
+     * @since 2.2
+     */
+    public static boolean equalsIncludingNaN(float x, float y, float eps) {
+        return equalsIncludingNaN(x, y) || (FastMath.abs(y - x) <= eps);
+    }
+
+    /**
+     * Returns true if both arguments are equal or within the range of allowed
+     * error (inclusive).
+     * Two float numbers are considered equal if there are {@code (maxUlps - 1)}
+     * (or fewer) floating point numbers between them, i.e. two adjacent floating
+     * point numbers are considered equal.
+     * Adapted from <a
+     * href="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm">
+     * Bruce Dawson</a>
+     *
+     * @param x first value
+     * @param y second value
+     * @param maxUlps {@code (maxUlps - 1)} is the number of floating point
+     * values between {@code x} and {@code y}.
+     * @return {@code true} if there are fewer than {@code maxUlps} floating
+     * point values between {@code x} and {@code y}.
+     * @since 2.2
+     */
+    public static boolean equals(float x, float y, int maxUlps) {
+        int xInt = Float.floatToIntBits(x);
+        int yInt = Float.floatToIntBits(y);
+
+        // Make lexicographically ordered as a two's-complement integer.
+        if (xInt < 0) {
+            xInt = SGN_MASK_FLOAT - xInt;
+        }
+        if (yInt < 0) {
+            yInt = SGN_MASK_FLOAT - yInt;
+        }
+
+        final boolean isEqual = FastMath.abs(xInt - yInt) <= maxUlps;
+
+        return isEqual && !Float.isNaN(x) && !Float.isNaN(y);
+    }
+
+    /**
+     * Returns true if both arguments are NaN or if they are equal as defined
+     * by {@link #equals(float,float,int) equals(x, y, maxUlps)}.
+     *
+     * @param x first value
+     * @param y second value
+     * @param maxUlps {@code (maxUlps - 1)} is the number of floating point
+     * values between {@code x} and {@code y}.
+     * @return {@code true} if both arguments are NaN or if there are less than
+     * {@code maxUlps} floating point values between {@code x} and {@code y}.
+     * @since 2.2
+     */
+    public static boolean equalsIncludingNaN(float x, float y, int maxUlps) {
+        return (Float.isNaN(x) && Float.isNaN(y)) || equals(x, y, maxUlps);
+    }
+
+    /**
+     * Returns true iff they are equal as defined by
+     * {@link #equals(double,double,int) equals(x, y, 1)}.
+     *
+     * @param x first value
+     * @param y second value
+     * @return {@code true} if the values are equal.
+     */
+    public static boolean equals(double x, double y) {
+        return equals(x, y, 1);
+    }
+
+    /**
+     * Returns true if both arguments are NaN or neither is NaN and they are
+     * equal as defined by {@link #equals(double,double) equals(x, y, 1)}.
+     *
+     * @param x first value
+     * @param y second value
+     * @return {@code true} if the values are equal or both are NaN.
+     * @since 2.2
+     */
+    public static boolean equalsIncludingNaN(double x, double y) {
+        return (Double.isNaN(x) && Double.isNaN(y)) || equals(x, y, 1);
+    }
+
+    /**
+     * Returns {@code true} if there is no double value strictly between the
+     * arguments or the difference between them is within the range of allowed
+     * error (inclusive).
+     *
+     * @param x First value.
+     * @param y Second value.
+     * @param eps Amount of allowed absolute error.
+     * @return {@code true} if the values are two adjacent floating point
+     * numbers or they are within range of each other.
+     */
+    public static boolean equals(double x, double y, double eps) {
+        return equals(x, y, 1) || FastMath.abs(y - x) <= eps;
+    }
+
+    /**
+     * Returns true if both arguments are NaN or are equal or within the range
+     * of allowed error (inclusive).
+     *
+     * @param x first value
+     * @param y second value
+     * @param eps the amount of absolute error to allow.
+     * @return {@code true} if the values are equal or within range of each other,
+     * or both are NaN.
+     * @since 2.2
+     */
+    public static boolean equalsIncludingNaN(double x, double y, double eps) {
+        return equalsIncludingNaN(x, y) || (FastMath.abs(y - x) <= eps);
+    }
+
+    /**
+     * Returns true if both arguments are equal or within the range of allowed
+     * error (inclusive).
+     * Two float numbers are considered equal if there are {@code (maxUlps - 1)}
+     * (or fewer) floating point numbers between them, i.e. two adjacent floating
+     * point numbers are considered equal.
+     * Adapted from <a
+     * href="http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm">
+     * Bruce Dawson</a>
+     *
+     * @param x first value
+     * @param y second value
+     * @param maxUlps {@code (maxUlps - 1)} is the number of floating point
+     * values between {@code x} and {@code y}.
+     * @return {@code true} if there are fewer than {@code maxUlps} floating
+     * point values between {@code x} and {@code y}.
+     */
+    public static boolean equals(double x, double y, int maxUlps) {
+        long xInt = Double.doubleToLongBits(x);
+        long yInt = Double.doubleToLongBits(y);
+
+        // Make lexicographically ordered as a two's-complement integer.
+        if (xInt < 0) {
+            xInt = SGN_MASK - xInt;
+        }
+        if (yInt < 0) {
+            yInt = SGN_MASK - yInt;
+        }
+
+        final boolean isEqual = FastMath.abs(xInt - yInt) <= maxUlps;
+
+        return isEqual && !Double.isNaN(x) && !Double.isNaN(y);
+    }
+
+    /**
+     * Returns true if both arguments are NaN or if they are equal as defined
+     * by {@link #equals(double,double,int) equals(x, y, maxUlps)}.
+     *
+     * @param x first value
+     * @param y second value
+     * @param maxUlps {@code (maxUlps - 1)} is the number of floating point
+     * values between {@code x} and {@code y}.
+     * @return {@code true} if both arguments are NaN or if there are less than
+     * {@code maxUlps} floating point values between {@code x} and {@code y}.
+     * @since 2.2
+     */
+    public static boolean equalsIncludingNaN(double x, double y, int maxUlps) {
+        return (Double.isNaN(x) && Double.isNaN(y)) || equals(x, y, maxUlps);
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/Precision.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/TestUtils.java Mon Oct 10 22:35:54 2011
@@ -33,7 +33,7 @@ import org.apache.commons.math.linear.Re
 import org.apache.commons.math.stat.inference.ChiSquareTest;
 import org.apache.commons.math.stat.inference.ChiSquareTestImpl;
 import org.apache.commons.math.util.FastMath;
-import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 import org.junit.Assert;
 
 /**
@@ -190,8 +190,8 @@ public class TestUtils {
     public static void assertContains(String msg, Complex[] values,
                                       Complex z, double epsilon) {
         for (Complex value : values) {
-            if (MathUtils.equals(value.getReal(), z.getReal(), epsilon) &&
-                MathUtils.equals(value.getImaginary(), z.getImaginary(), epsilon)) {
+            if (Precision.equals(value.getReal(), z.getReal(), epsilon) &&
+                Precision.equals(value.getImaginary(), z.getImaginary(), epsilon)) {
                 return;
             }
         }
@@ -221,7 +221,7 @@ public class TestUtils {
     public static void assertContains(String msg, double[] values,
             double x, double epsilon) {
         for (double value : values) {
-            if (MathUtils.equals(value, x, epsilon)) {
+            if (Precision.equals(value, x, epsilon)) {
                 return;
             }
         }
@@ -305,7 +305,7 @@ public class TestUtils {
         }
         boolean failure = false;
         for (int i=0; i < expected.length; i++) {
-            if (!MathUtils.equalsIncludingNaN(expected[i], observed[i], tolerance)) {
+            if (!Precision.equalsIncludingNaN(expected[i], observed[i], tolerance)) {
                 failure = true;
                 out.append("\n Elements at index ");
                 out.append(i);

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtilsTest.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtilsTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialsUtilsTest.java Mon Oct 10 22:35:54 2011
@@ -20,6 +20,7 @@ import org.apache.commons.math.analysis.
 import org.apache.commons.math.analysis.integration.LegendreGaussIntegrator;
 import org.apache.commons.math.util.FastMath;
 import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -289,7 +290,7 @@ public class PolynomialsUtilsTest {
                 for (int i = 0; i < 10; ++i) {
                     PolynomialFunction jacobi = PolynomialsUtils.createJacobiPolynomial(i, v, w);
                     double binomial = MathUtils.binomialCoefficient(v + i, i);
-                    Assert.assertTrue(MathUtils.equals(binomial, jacobi.value(1.0), 1));
+                    Assert.assertTrue(Precision.equals(binomial, jacobi.value(1.0), 1));
                 }
             }
         }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ExponentialDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ExponentialDistributionTest.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ExponentialDistributionTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/distribution/ExponentialDistributionTest.java Mon Oct 10 22:35:54 2011
@@ -17,7 +17,7 @@
 package org.apache.commons.math.distribution;
 
 import org.apache.commons.math.util.FastMath;
-import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 import org.apache.commons.math.exception.NotStrictlyPositiveException;
 import org.junit.Assert;
 import org.junit.Test;
@@ -93,14 +93,14 @@ public class ExponentialDistributionTest
     @Test
     public void testDensity() {
         ExponentialDistribution d1 = new ExponentialDistributionImpl(1);
-        Assert.assertTrue(MathUtils.equals(0.0, d1.density(-1e-9), 1));
-        Assert.assertTrue(MathUtils.equals(1.0, d1.density(0.0), 1));
-        Assert.assertTrue(MathUtils.equals(0.0, d1.density(1000.0), 1));
-        Assert.assertTrue(MathUtils.equals(FastMath.exp(-1), d1.density(1.0), 1));
-        Assert.assertTrue(MathUtils.equals(FastMath.exp(-2), d1.density(2.0), 1));
+        Assert.assertTrue(Precision.equals(0.0, d1.density(-1e-9), 1));
+        Assert.assertTrue(Precision.equals(1.0, d1.density(0.0), 1));
+        Assert.assertTrue(Precision.equals(0.0, d1.density(1000.0), 1));
+        Assert.assertTrue(Precision.equals(FastMath.exp(-1), d1.density(1.0), 1));
+        Assert.assertTrue(Precision.equals(FastMath.exp(-2), d1.density(2.0), 1));
 
         ExponentialDistribution d2 = new ExponentialDistributionImpl(3);
-        Assert.assertTrue(MathUtils.equals(1/3.0, d2.density(0.0), 1));
+        Assert.assertTrue(Precision.equals(1/3.0, d2.density(0.0), 1));
         // computed using  print(dexp(1, rate=1/3), digits=10) in R 2.5
         Assert.assertEquals(0.2388437702, d2.density(1.0), 1e-8);
 

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/filter/KalmanFilterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/filter/KalmanFilterTest.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/filter/KalmanFilterTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/filter/KalmanFilterTest.java Mon Oct 10 22:35:54 2011
@@ -20,7 +20,7 @@ import org.apache.commons.math.linear.Re
 import org.apache.commons.math.linear.RealVector;
 import org.apache.commons.math.random.JDKRandomGenerator;
 import org.apache.commons.math.random.RandomGenerator;
-import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -89,11 +89,11 @@ public class KalmanFilterTest {
             // state estimate should be larger than measurement noise
             double diff = Math.abs(constantValue - filter.getStateEstimation()[0]);
             // System.out.println(diff);
-            Assert.assertTrue(MathUtils.compareTo(diff, measurementNoise, 1e-6) < 0);
+            Assert.assertTrue(Precision.compareTo(diff, measurementNoise, 1e-6) < 0);
         }
 
         // error covariance should be already very low (< 0.02)
-        Assert.assertTrue(MathUtils.compareTo(filter.getErrorCovariance()[0][0],
+        Assert.assertTrue(Precision.compareTo(filter.getErrorCovariance()[0][0],
                                               0.02d, 1e-6) < 0);
     }
 

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/linear/SimplexSolverTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/linear/SimplexSolverTest.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/linear/SimplexSolverTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/linear/SimplexSolverTest.java Mon Oct 10 22:35:54 2011
@@ -24,7 +24,7 @@ import java.util.Collection;
 
 import org.apache.commons.math.optimization.GoalType;
 import org.apache.commons.math.optimization.RealPointValuePair;
-import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 import org.junit.Test;
 
 public class SimplexSolverTest {
@@ -72,7 +72,7 @@ public class SimplexSolverTest {
         SimplexSolver solver = new SimplexSolver();
         RealPointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, false);
         
-        Assert.assertTrue(MathUtils.compareTo(solution.getPoint()[0] * 200.d, 1.d, epsilon) >= 0);
+        Assert.assertTrue(Precision.compareTo(solution.getPoint()[0] * 200.d, 1.d, epsilon) >= 0);
         Assert.assertEquals(0.0050, solution.getValue(), epsilon);
     }
 
@@ -95,7 +95,7 @@ public class SimplexSolverTest {
         SimplexSolver simplex = new SimplexSolver();
         RealPointValuePair solution = simplex.optimize(f, constraints, GoalType.MINIMIZE, false);
         
-        Assert.assertTrue(MathUtils.compareTo(solution.getPoint()[0], -1e-18d, epsilon) >= 0);
+        Assert.assertTrue(Precision.compareTo(solution.getPoint()[0], -1e-18d, epsilon) >= 0);
         Assert.assertEquals(1.0d, solution.getPoint()[1], epsilon);        
         Assert.assertEquals(0.0d, solution.getPoint()[2], epsilon);
         Assert.assertEquals(1.0d, solution.getValue(), epsilon);

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/StatUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/StatUtilsTest.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/StatUtilsTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/StatUtilsTest.java Mon Oct 10 22:35:54 2011
@@ -20,7 +20,7 @@ package org.apache.commons.math.stat;
 import org.apache.commons.math.TestUtils;
 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
 import org.apache.commons.math.util.FastMath;
-import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 import org.apache.commons.math.exception.MathIllegalArgumentException;
 import org.junit.Assert;
 import org.junit.Test;
@@ -445,7 +445,7 @@ public final class StatUtilsTest {
         double expectedSample[] = { -25 / Math.sqrt(1250), 25 / Math.sqrt(1250) };
         double[] out = StatUtils.normalize(sample);
         for (int i = 0; i < out.length; i++) {
-            Assert.assertTrue(MathUtils.equals(out[i], expectedSample[i], 1));
+            Assert.assertTrue(Precision.equals(out[i], expectedSample[i], 1));
         }
 
     }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/AggregateSummaryStatisticsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/AggregateSummaryStatisticsTest.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/AggregateSummaryStatisticsTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/AggregateSummaryStatisticsTest.java Mon Oct 10 22:35:54 2011
@@ -24,7 +24,7 @@ import java.util.Collection;
 import org.apache.commons.math.TestUtils;
 import org.apache.commons.math.random.RandomData;
 import org.apache.commons.math.random.RandomDataImpl;
-import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -54,16 +54,16 @@ public class AggregateSummaryStatisticsT
         setOneStats.addValue(7);
         setOneStats.addValue(11);
         Assert.assertEquals("Wrong number of set one values", 5, setOneStats.getN());
-        Assert.assertTrue("Wrong sum of set one values", MathUtils.equals(28.0, setOneStats.getSum(), 1));
+        Assert.assertTrue("Wrong sum of set one values", Precision.equals(28.0, setOneStats.getSum(), 1));
 
         setTwoStats.addValue(2);
         setTwoStats.addValue(4);
         setTwoStats.addValue(8);
         Assert.assertEquals("Wrong number of set two values", 3, setTwoStats.getN());
-        Assert.assertTrue("Wrong sum of set two values", MathUtils.equals(14.0, setTwoStats.getSum(), 1));
+        Assert.assertTrue("Wrong sum of set two values", Precision.equals(14.0, setTwoStats.getSum(), 1));
 
         Assert.assertEquals("Wrong number of aggregate values", 8, aggregate.getN());
-        Assert.assertTrue("Wrong aggregate sum", MathUtils.equals(42.0, aggregate.getSum(), 1));
+        Assert.assertTrue("Wrong aggregate sum", Precision.equals(42.0, aggregate.getSum(), 1));
     }
 
     /**

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/DescriptiveStatisticsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/DescriptiveStatisticsTest.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/DescriptiveStatisticsTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/DescriptiveStatisticsTest.java Mon Oct 10 22:35:54 2011
@@ -17,7 +17,7 @@ import java.util.Locale;
 
 
 import org.apache.commons.math.stat.descriptive.rank.Percentile;
-import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -214,11 +214,11 @@ public class DescriptiveStatisticsTest {
             dstat.addValue(i);
         }
 
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(mean1, dstat.getMean()));
+        Assert.assertTrue(Precision.equalsIncludingNaN(mean1, dstat.getMean()));
         dstat.replaceMostRecentValue(0);
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(mean2, dstat.getMean()));
+        Assert.assertTrue(Precision.equalsIncludingNaN(mean2, dstat.getMean()));
         dstat.removeMostRecentValue();
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(mean3, dstat.getMean()));
+        Assert.assertTrue(Precision.equalsIncludingNaN(mean3, dstat.getMean()));
 
     }
 

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/transform/FastHadamardTransformerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/transform/FastHadamardTransformerTest.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/transform/FastHadamardTransformerTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/transform/FastHadamardTransformerTest.java Mon Oct 10 22:35:54 2011
@@ -16,7 +16,7 @@
  */
 package org.apache.commons.math.transform;
 
-import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.util.Precision;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -89,7 +89,7 @@ public final class FastHadamardTransform
         double dResult[] = transformer.transform(dX);
         for (int i = 0; i < dResult.length; i++) {
             // compare computed results to precomputed results
-            Assert.assertTrue(MathUtils.equals((double) y[i], dResult[i], 1));
+            Assert.assertTrue(Precision.equals((double) y[i], dResult[i], 1));
         }
     }
 
@@ -118,7 +118,7 @@ public final class FastHadamardTransform
         double dResult[] = transformer.inversetransform(dY);
         for (int i = 0; i < dResult.length; i++) {
             // compare computed results to precomputed results
-            Assert.assertTrue(MathUtils.equals((double) x[i], dResult[i], 1));
+            Assert.assertTrue(Precision.equals((double) x[i], dResult[i], 1));
         }
 
     }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java Mon Oct 10 22:35:54 2011
@@ -310,53 +310,6 @@ public final class MathUtilsTest {
     }
 
     @Test
-    public void testCompareToEpsilon() {
-        Assert.assertEquals(0, MathUtils.compareTo(152.33, 152.32, .011));
-        Assert.assertTrue(MathUtils.compareTo(152.308, 152.32, .011) < 0);
-        Assert.assertTrue(MathUtils.compareTo(152.33, 152.318, .011) > 0);
-        Assert.assertEquals(0, MathUtils.compareTo(Double.MIN_VALUE, +0.0, Double.MIN_VALUE));
-        Assert.assertEquals(0, MathUtils.compareTo(Double.MIN_VALUE, -0.0, Double.MIN_VALUE));
-    }
-
-    @Test
-    public void testCompareToMaxUlps() {
-        double a     = 152.32;
-        double delta = FastMath.ulp(a);
-        for (int i = 0; i <= 10; ++i) {
-            if (i <= 5) {
-                Assert.assertEquals( 0, MathUtils.compareTo(a, a + i * delta, 5));
-                Assert.assertEquals( 0, MathUtils.compareTo(a, a - i * delta, 5));
-            } else {
-                Assert.assertEquals(-1, MathUtils.compareTo(a, a + i * delta, 5));
-                Assert.assertEquals(+1, MathUtils.compareTo(a, a - i * delta, 5));
-            }
-        }
-
-        Assert.assertEquals( 0, MathUtils.compareTo(-0.0, 0.0, 0));
-
-        Assert.assertEquals(-1, MathUtils.compareTo(-Double.MIN_VALUE, -0.0, 0));
-        Assert.assertEquals( 0, MathUtils.compareTo(-Double.MIN_VALUE, -0.0, 1));
-        Assert.assertEquals(-1, MathUtils.compareTo(-Double.MIN_VALUE, +0.0, 0));
-        Assert.assertEquals( 0, MathUtils.compareTo(-Double.MIN_VALUE, +0.0, 1));
-
-        Assert.assertEquals(+1, MathUtils.compareTo( Double.MIN_VALUE, -0.0, 0));
-        Assert.assertEquals( 0, MathUtils.compareTo( Double.MIN_VALUE, -0.0, 1));
-        Assert.assertEquals(+1, MathUtils.compareTo( Double.MIN_VALUE, +0.0, 0));
-        Assert.assertEquals( 0, MathUtils.compareTo( Double.MIN_VALUE, +0.0, 1));
-
-        Assert.assertEquals(-1, MathUtils.compareTo(-Double.MIN_VALUE, Double.MIN_VALUE, 0));
-        Assert.assertEquals(-1, MathUtils.compareTo(-Double.MIN_VALUE, Double.MIN_VALUE, 1));
-        Assert.assertEquals( 0, MathUtils.compareTo(-Double.MIN_VALUE, Double.MIN_VALUE, 2));
-
-        Assert.assertEquals( 0, MathUtils.compareTo(Double.MAX_VALUE, Double.POSITIVE_INFINITY, 1));
-        Assert.assertEquals(-1, MathUtils.compareTo(Double.MAX_VALUE, Double.POSITIVE_INFINITY, 0));
-
-        Assert.assertEquals(+1, MathUtils.compareTo(Double.MAX_VALUE, Double.NaN, Integer.MAX_VALUE));
-        Assert.assertEquals(+1, MathUtils.compareTo(Double.NaN, Double.MAX_VALUE, Integer.MAX_VALUE));
-
-    }
-
-    @Test
     public void testCosh() {
         double x = 3.0;
         double expected = 10.06766;
@@ -369,192 +322,6 @@ public final class MathUtilsTest {
     }
 
     @Test
-    public void testEqualsIncludingNaN() {
-        double[] testArray = {
-            Double.NaN,
-            Double.POSITIVE_INFINITY,
-            Double.NEGATIVE_INFINITY,
-            1d,
-            0d };
-        for (int i = 0; i < testArray.length; i++) {
-            for (int j = 0; j < testArray.length; j++) {
-                if (i == j) {
-                    Assert.assertTrue(MathUtils.equalsIncludingNaN(testArray[i], testArray[j]));
-                    Assert.assertTrue(MathUtils.equalsIncludingNaN(testArray[j], testArray[i]));
-                } else {
-                    Assert.assertTrue(!MathUtils.equalsIncludingNaN(testArray[i], testArray[j]));
-                    Assert.assertTrue(!MathUtils.equalsIncludingNaN(testArray[j], testArray[i]));
-                }
-            }
-        }
-    }
-
-    @Test
-    public void testEqualsWithAllowedDelta() {
-        Assert.assertTrue(MathUtils.equals(153.0000, 153.0000, .0625));
-        Assert.assertTrue(MathUtils.equals(153.0000, 153.0625, .0625));
-        Assert.assertTrue(MathUtils.equals(152.9375, 153.0000, .0625));
-        Assert.assertFalse(MathUtils.equals(153.0000, 153.0625, .0624));
-        Assert.assertFalse(MathUtils.equals(152.9374, 153.0000, .0625));
-        Assert.assertFalse(MathUtils.equals(Double.NaN, Double.NaN, 1.0));
-        Assert.assertTrue(MathUtils.equals(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0));
-        Assert.assertTrue(MathUtils.equals(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 1.0));
-        Assert.assertFalse(MathUtils.equals(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0));
-    }
-
-    @Test
-    public void testMath475() {
-        final double a = 1.7976931348623182E16;
-        final double b = FastMath.nextUp(a);
-
-        double diff = FastMath.abs(a - b);
-        // Because they are adjacent floating point numbers, "a" and "b" are
-        // considered equal even though the allowed error is smaller than
-        // their difference.
-        Assert.assertTrue(MathUtils.equals(a, b, 0.5 * diff));
-
-        final double c = FastMath.nextUp(b);
-        diff = FastMath.abs(a - c);
-        // Because "a" and "c" are not adjacent, the tolerance is taken into
-        // account for assessing equality.
-        Assert.assertTrue(MathUtils.equals(a, c, diff));
-        Assert.assertFalse(MathUtils.equals(a, c, (1 - 1e-16) * diff));
-    }
-
-    @Test
-    public void testEqualsIncludingNaNWithAllowedDelta() {
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(153.0000, 153.0000, .0625));
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(153.0000, 153.0625, .0625));
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(152.9375, 153.0000, .0625));
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(Double.NaN, Double.NaN, 1.0));
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0));
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 1.0));
-        Assert.assertFalse(MathUtils.equalsIncludingNaN(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0));
-        Assert.assertFalse(MathUtils.equalsIncludingNaN(153.0000, 153.0625, .0624));
-        Assert.assertFalse(MathUtils.equalsIncludingNaN(152.9374, 153.0000, .0625));
-    }
-
-    // Tests for floating point equality
-    @Test
-    public void testFloatEqualsWithAllowedUlps() {
-        Assert.assertTrue("+0.0f == -0.0f",MathUtils.equals(0.0f, -0.0f));
-        Assert.assertTrue("+0.0f == -0.0f (1 ulp)",MathUtils.equals(0.0f, -0.0f, 1));
-        float oneFloat = 1.0f;
-        Assert.assertTrue("1.0f == 1.0f + 1 ulp",MathUtils.equals(oneFloat, Float.intBitsToFloat(1 + Float.floatToIntBits(oneFloat))));
-        Assert.assertTrue("1.0f == 1.0f + 1 ulp (1 ulp)",MathUtils.equals(oneFloat, Float.intBitsToFloat(1 + Float.floatToIntBits(oneFloat)), 1));
-        Assert.assertFalse("1.0f != 1.0f + 2 ulp (1 ulp)",MathUtils.equals(oneFloat, Float.intBitsToFloat(2 + Float.floatToIntBits(oneFloat)), 1));
-
-        Assert.assertTrue(MathUtils.equals(153.0f, 153.0f, 1));
-
-        // These tests need adjusting for floating point precision
-//        Assert.assertTrue(MathUtils.equals(153.0f, 153.00000000000003f, 1));
-//        Assert.assertFalse(MathUtils.equals(153.0f, 153.00000000000006f, 1));
-//        Assert.assertTrue(MathUtils.equals(153.0f, 152.99999999999997f, 1));
-//        Assert.assertFalse(MathUtils.equals(153f, 152.99999999999994f, 1));
-//
-//        Assert.assertTrue(MathUtils.equals(-128.0f, -127.99999999999999f, 1));
-//        Assert.assertFalse(MathUtils.equals(-128.0f, -127.99999999999997f, 1));
-//        Assert.assertTrue(MathUtils.equals(-128.0f, -128.00000000000003f, 1));
-//        Assert.assertFalse(MathUtils.equals(-128.0f, -128.00000000000006f, 1));
-
-        Assert.assertTrue(MathUtils.equals(Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, 1));
-        Assert.assertTrue(MathUtils.equals(Double.MAX_VALUE, Float.POSITIVE_INFINITY, 1));
-
-        Assert.assertTrue(MathUtils.equals(Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, 1));
-        Assert.assertTrue(MathUtils.equals(-Float.MAX_VALUE, Float.NEGATIVE_INFINITY, 1));
-
-        Assert.assertFalse(MathUtils.equals(Float.NaN, Float.NaN, 1));
-        Assert.assertFalse(MathUtils.equals(Float.NaN, Float.NaN, 0));
-        Assert.assertFalse(MathUtils.equals(Float.NaN, 0, 0));
-        Assert.assertFalse(MathUtils.equals(Float.NaN, Float.POSITIVE_INFINITY, 0));
-        Assert.assertFalse(MathUtils.equals(Float.NaN, Float.NEGATIVE_INFINITY, 0));
-
-        Assert.assertFalse(MathUtils.equals(Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, 100000));
-    }
-
-    @Test
-    public void testEqualsWithAllowedUlps() {
-        Assert.assertTrue(MathUtils.equals(0.0, -0.0, 1));
-
-        Assert.assertTrue(MathUtils.equals(1.0, 1 + FastMath.ulp(1d), 1));
-        Assert.assertFalse(MathUtils.equals(1.0, 1 + 2 * FastMath.ulp(1d), 1));
-
-        final double nUp1 = FastMath.nextAfter(1d, Double.POSITIVE_INFINITY);
-        final double nnUp1 = FastMath.nextAfter(nUp1, Double.POSITIVE_INFINITY);
-        Assert.assertTrue(MathUtils.equals(1.0, nUp1, 1));
-        Assert.assertTrue(MathUtils.equals(nUp1, nnUp1, 1));
-        Assert.assertFalse(MathUtils.equals(1.0, nnUp1, 1));
-
-        Assert.assertTrue(MathUtils.equals(0.0, FastMath.ulp(0d), 1));
-        Assert.assertTrue(MathUtils.equals(0.0, -FastMath.ulp(0d), 1));
-
-        Assert.assertTrue(MathUtils.equals(153.0, 153.0, 1));
-
-        Assert.assertTrue(MathUtils.equals(153.0, 153.00000000000003, 1));
-        Assert.assertFalse(MathUtils.equals(153.0, 153.00000000000006, 1));
-        Assert.assertTrue(MathUtils.equals(153.0, 152.99999999999997, 1));
-        Assert.assertFalse(MathUtils.equals(153, 152.99999999999994, 1));
-
-        Assert.assertTrue(MathUtils.equals(-128.0, -127.99999999999999, 1));
-        Assert.assertFalse(MathUtils.equals(-128.0, -127.99999999999997, 1));
-        Assert.assertTrue(MathUtils.equals(-128.0, -128.00000000000003, 1));
-        Assert.assertFalse(MathUtils.equals(-128.0, -128.00000000000006, 1));
-
-        Assert.assertTrue(MathUtils.equals(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1));
-        Assert.assertTrue(MathUtils.equals(Double.MAX_VALUE, Double.POSITIVE_INFINITY, 1));
-
-        Assert.assertTrue(MathUtils.equals(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
-        Assert.assertTrue(MathUtils.equals(-Double.MAX_VALUE, Double.NEGATIVE_INFINITY, 1));
-
-        Assert.assertFalse(MathUtils.equals(Double.NaN, Double.NaN, 1));
-        Assert.assertFalse(MathUtils.equals(Double.NaN, Double.NaN, 0));
-        Assert.assertFalse(MathUtils.equals(Double.NaN, 0, 0));
-        Assert.assertFalse(MathUtils.equals(Double.NaN, Double.POSITIVE_INFINITY, 0));
-        Assert.assertFalse(MathUtils.equals(Double.NaN, Double.NEGATIVE_INFINITY, 0));
-
-        Assert.assertFalse(MathUtils.equals(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 100000));
-    }
-
-    @Test
-    public void testEqualsIncludingNaNWithAllowedUlps() {
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(0.0, -0.0, 1));
-
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(1.0, 1 + FastMath.ulp(1d), 1));
-        Assert.assertFalse(MathUtils.equalsIncludingNaN(1.0, 1 + 2 * FastMath.ulp(1d), 1));
-
-        final double nUp1 = FastMath.nextAfter(1d, Double.POSITIVE_INFINITY);
-        final double nnUp1 = FastMath.nextAfter(nUp1, Double.POSITIVE_INFINITY);
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(1.0, nUp1, 1));
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(nUp1, nnUp1, 1));
-        Assert.assertFalse(MathUtils.equalsIncludingNaN(1.0, nnUp1, 1));
-
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(0.0, FastMath.ulp(0d), 1));
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(0.0, -FastMath.ulp(0d), 1));
-
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(153.0, 153.0, 1));
-
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(153.0, 153.00000000000003, 1));
-        Assert.assertFalse(MathUtils.equalsIncludingNaN(153.0, 153.00000000000006, 1));
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(153.0, 152.99999999999997, 1));
-        Assert.assertFalse(MathUtils.equalsIncludingNaN(153, 152.99999999999994, 1));
-
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(-128.0, -127.99999999999999, 1));
-        Assert.assertFalse(MathUtils.equalsIncludingNaN(-128.0, -127.99999999999997, 1));
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(-128.0, -128.00000000000003, 1));
-        Assert.assertFalse(MathUtils.equalsIncludingNaN(-128.0, -128.00000000000006, 1));
-
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1));
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(Double.MAX_VALUE, Double.POSITIVE_INFINITY, 1));
-
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 1));
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(-Double.MAX_VALUE, Double.NEGATIVE_INFINITY, 1));
-
-        Assert.assertTrue(MathUtils.equalsIncludingNaN(Double.NaN, Double.NaN, 1));
-
-        Assert.assertFalse(MathUtils.equalsIncludingNaN(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 100000));
-    }
-
-    @Test
     public void testArrayEquals() {
         Assert.assertFalse(MathUtils.equals(new double[] { 1d }, null));
         Assert.assertFalse(MathUtils.equals(null, new double[] { 1d }));
@@ -1573,7 +1340,7 @@ public final class MathUtilsTest {
     public void testL1DistanceDouble() {
         double[] p1 = { 2.5,  0.0 };
         double[] p2 = { -0.5, 4.0 };
-        Assert.assertTrue(MathUtils.equals(7.0, MathUtils.distance1(p1, p2), 1));
+        Assert.assertTrue(Precision.equals(7.0, MathUtils.distance1(p1, p2), 1));
     }
 
     @Test
@@ -1587,21 +1354,21 @@ public final class MathUtilsTest {
     public void testL2DistanceDouble() {
         double[] p1 = { 2.5,  0.0 };
         double[] p2 = { -0.5, 4.0 };
-        Assert.assertTrue(MathUtils.equals(5.0, MathUtils.distance(p1, p2), 1));
+        Assert.assertTrue(Precision.equals(5.0, MathUtils.distance(p1, p2), 1));
     }
 
     @Test
     public void testL2DistanceInt() {
         int[] p1 = { 3, 0 };
         int[] p2 = { 0, 4 };
-        Assert.assertTrue(MathUtils.equals(5, MathUtils.distance(p1, p2), 1));
+        Assert.assertTrue(Precision.equals(5, MathUtils.distance(p1, p2), 1));
     }
 
     @Test
     public void testLInfDistanceDouble() {
         double[] p1 = { 2.5,  0.0 };
         double[] p2 = { -0.5, 4.0 };
-        Assert.assertTrue(MathUtils.equals(4.0, MathUtils.distanceInf(p1, p2), 1));
+        Assert.assertTrue(Precision.equals(4.0, MathUtils.distanceInf(p1, p2), 1));
     }
 
     @Test

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/OpenIntToDoubleHashMapTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/OpenIntToDoubleHashMapTest.java?rev=1181282&r1=1181281&r2=1181282&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/OpenIntToDoubleHashMapTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/OpenIntToDoubleHashMapTest.java Mon Oct 10 22:35:54 2011
@@ -102,7 +102,7 @@ public class OpenIntToDoubleHashMapTest 
             if (!keysInMap.contains(mapEntry.getKey()))
                 ++mapSize;
             Assert.assertEquals(mapSize, map.size());
-            Assert.assertTrue(MathUtils.equals(mapEntry.getValue(), map.get(mapEntry.getKey()), 1));
+            Assert.assertTrue(Precision.equals(mapEntry.getValue(), map.get(mapEntry.getKey()), 1));
         }
     }
 
@@ -113,7 +113,7 @@ public class OpenIntToDoubleHashMapTest 
         for (Map.Entry<Integer, Double> mapEntry : generateAbsent().entrySet()) {
             map.put(mapEntry.getKey(), mapEntry.getValue());
             Assert.assertEquals(++size, map.size());
-            Assert.assertTrue(MathUtils.equals(mapEntry.getValue(), map.get(mapEntry.getKey()), 1));
+            Assert.assertTrue(Precision.equals(mapEntry.getValue(), map.get(mapEntry.getKey()), 1));
         }
     }
 
@@ -123,7 +123,7 @@ public class OpenIntToDoubleHashMapTest 
         for (Map.Entry<Integer, Double> mapEntry : javaMap.entrySet()) {
             map.put(mapEntry.getKey(), mapEntry.getValue());
             Assert.assertEquals(javaMap.size(), map.size());
-            Assert.assertTrue(MathUtils.equals(mapEntry.getValue(), map.get(mapEntry.getKey()), 1));
+            Assert.assertTrue(Precision.equals(mapEntry.getValue(), map.get(mapEntry.getKey()), 1));
         }
     }
 
@@ -219,7 +219,7 @@ public class OpenIntToDoubleHashMapTest 
         Assert.assertEquals(javaMap.size(), copy.size());
 
         for (Map.Entry<Integer, Double> mapEntry : javaMap.entrySet())
-            Assert.assertTrue(MathUtils.equals(mapEntry.getValue(), copy.get(mapEntry.getKey()), 1));
+            Assert.assertTrue(Precision.equals(mapEntry.getValue(), copy.get(mapEntry.getKey()), 1));
     }
 
     @Test
@@ -289,13 +289,13 @@ public class OpenIntToDoubleHashMapTest 
         map.put(key2, value1);
         int key3 = 1008859686;
         map.put(key3, value1);
-        Assert.assertTrue(MathUtils.equals(value1, map.get(key3), 1));
+        Assert.assertTrue(Precision.equals(value1, map.get(key3), 1));
         Assert.assertEquals(3, map.size());
 
         map.remove(key2);
         double value2 = 2.0;
         map.put(key3, value2);
-        Assert.assertTrue(MathUtils.equals(value2, map.get(key3), 1));
+        Assert.assertTrue(Precision.equals(value2, map.get(key3), 1));
         Assert.assertEquals(2, map.size());
     }
 
@@ -312,13 +312,13 @@ public class OpenIntToDoubleHashMapTest 
         int key2 = 476463321;
         map.put(key2, value1);
         Assert.assertEquals(2, map.size());
-        Assert.assertTrue(MathUtils.equals(value1, map.get(key2), 1));
+        Assert.assertTrue(Precision.equals(value1, map.get(key2), 1));
 
         map.remove(key1);
         double value2 = 2.0;
         map.put(key2, value2);
         Assert.assertEquals(1, map.size());
-        Assert.assertTrue(MathUtils.equals(value2, map.get(key2), 1));
+        Assert.assertTrue(Precision.equals(value2, map.get(key2), 1));
     }
 
 }



Mime
View raw message