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/02/21 21:16:37 UTC
svn commit: r746582 - 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: Sat Feb 21 20:16:36 2009
New Revision: 746582
URL: http://svn.apache.org/viewvc?rev=746582&view=rev
Log:
handle NaN and infinities correctly in the MathUtils.equals method with tolerance
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=746582&r1=746581&r2=746582&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 Sat Feb 21 20:16:36 2009
@@ -373,6 +373,9 @@
/**
* Returns true iff both arguments are equal or within the range of allowed
* error (inclusive).
+ * <p>
+ * Two NaNs are considered equals, as are two infinities with same size.
+ * </p>
*
* @param x first value
* @param y second value
@@ -380,7 +383,7 @@
* @return true if the values are equal or within range of each other
*/
public static boolean equals(double x, double y, double eps) {
- return x == y || (x < y && (x + eps) >= y) || (x > y && x <= (y + eps));
+ return equals(x, y) || (Math.abs(y - x) <= eps);
}
/**
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=746582&r1=746581&r2=746582&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 Sat Feb 21 20:16:36 2009
@@ -336,11 +336,15 @@
}
public void testEqualsWithAllowedDelta() {
- assertTrue(MathUtils.equals(153.0000, 153.0000, .0001));
- assertTrue(MathUtils.equals(153.0000, 153.0001, .0001));
- assertTrue(MathUtils.equals(152.9999, 153.0000, .0001));
- assertFalse(MathUtils.equals(153.0000, 153.0001, .00001));
- assertFalse(MathUtils.equals(152.9998, 153.0000, .0001));
+ assertTrue(MathUtils.equals(153.0000, 153.0000, .0625));
+ assertTrue(MathUtils.equals(153.0000, 153.0625, .0625));
+ assertTrue(MathUtils.equals(152.9375, 153.0000, .0625));
+ assertTrue(MathUtils.equals(Double.NaN, Double.NaN, 1.0));
+ assertTrue(MathUtils.equals(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0));
+ assertTrue(MathUtils.equals(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, 1.0));
+ assertFalse(MathUtils.equals(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, 1.0));
+ assertFalse(MathUtils.equals(153.0000, 153.0625, .0624));
+ assertFalse(MathUtils.equals(152.9374, 153.0000, .0625));
}
public void testArrayEquals() {