commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1059909 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/util/MathUtils.java test/java/org/apache/commons/math/util/MathUtilsTest.java
Date Mon, 17 Jan 2011 13:06:14 GMT
Author: erans
Date: Mon Jan 17 13:06:13 2011
New Revision: 1059909

URL: http://svn.apache.org/viewvc?rev=1059909&view=rev
Log:
MATH-475
Updated the Javadoc to make clear that 2 adjacent floating point numbers
are considered equal whatever the allowed tolerance.

Modified:
    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/util/MathUtilsTest.java

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=1059909&r1=1059908&r2=1059909&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
Jan 17 13:06:13 2011
@@ -596,13 +596,15 @@ public final class MathUtils {
     }
 
     /**
-     * Returns true if both arguments are equal or within the range of allowed
+     * 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 the amount of absolute error to allow.
-     * @return {@code true} if the values are equal or within range of each other.
+     * @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;

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=1059909&r1=1059908&r2=1059909&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 Jan 17 13:06:13 2011
@@ -343,12 +343,30 @@ public final class MathUtilsTest extends
         assertTrue(MathUtils.equals(153.0000, 153.0000, .0625));
         assertTrue(MathUtils.equals(153.0000, 153.0625, .0625));
         assertTrue(MathUtils.equals(152.9375, 153.0000, .0625));
+        assertFalse(MathUtils.equals(153.0000, 153.0625, .0624));
+        assertFalse(MathUtils.equals(152.9374, 153.0000, .0625));
         assertFalse(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 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.
+        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.
+        assertTrue(MathUtils.equals(a, c, diff));
+        assertFalse(MathUtils.equals(a, c, (1 - 1e-16) * diff));
     }
 
     public void testEqualsIncludingNaNWithAllowedDelta() {



Mime
View raw message