added compareTo method with epsilon

JIRA: MATH-247

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@749139 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2009-03-01 22:21:22 +00:00
parent 722fc97a7a
commit 4a64740b4a
2 changed files with 25 additions and 0 deletions

View File

@ -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 <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.

View File

@ -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;