You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2009/03/01 23:21:23 UTC

svn commit: r749139 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/util/MathUtils.java test/org/apache/commons/math/util/MathUtilsTest.java

Author: luc
Date: Sun Mar  1 22:21:22 2009
New Revision: 749139

URL: http://svn.apache.org/viewvc?rev=749139&view=rev
Log:
added compareTo method with epsilon
JIRA: MATH-247

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java
    commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java?rev=749139&r1=749138&r2=749139&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java Sun Mar  1 22:21:22 2009
@@ -348,6 +348,25 @@
     }
     
     /**
+     * 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;
+    }
+    
+    /**
      * Returns the <a href="http://mathworld.wolfram.com/HyperbolicCosine.html">
      * hyperbolic cosine</a> of x.
      * 

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java?rev=749139&r1=749138&r2=749139&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java Sun Mar  1 22:21:22 2009
@@ -305,6 +305,12 @@
             .isInfinite(x));
     }
 
+    public void testCompareTo() {
+      assertEquals(0, MathUtils.compareTo(152.33, 152.32, .011));
+      assertTrue(MathUtils.compareTo(152.308, 152.32, .011) < 0);
+      assertTrue(MathUtils.compareTo(152.33, 152.318, .011) > 0);
+    }
+    
     public void testCosh() {
         double x = 3.0;
         double expected = 10.06766;