diff --git a/src/java/org/apache/commons/math/util/MathUtils.java b/src/java/org/apache/commons/math/util/MathUtils.java
index 86717f74d..eeab0f3ee 100644
--- a/src/java/org/apache/commons/math/util/MathUtils.java
+++ b/src/java/org/apache/commons/math/util/MathUtils.java
@@ -347,6 +347,25 @@ public final class MathUtils {
return logSum;
}
+ /**
+ * 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
- 0 if {@link #equals(double, double, double) equals(x, y, eps)}
+ * - < 0 if !{@link #equals(double, double, double) equals(x, y, eps)} && x < y
+ * - > 0 if !{@link #equals(double, double, double) equals(x, y, eps)} && x > y
+ */
+ 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
* hyperbolic cosine of x.
diff --git a/src/test/org/apache/commons/math/util/MathUtilsTest.java b/src/test/org/apache/commons/math/util/MathUtilsTest.java
index 6e75859a9..ef354f353 100644
--- a/src/test/org/apache/commons/math/util/MathUtilsTest.java
+++ b/src/test/org/apache/commons/math/util/MathUtilsTest.java
@@ -305,6 +305,12 @@ public final class MathUtilsTest extends TestCase {
.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;