You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2014/04/15 14:22:31 UTC

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

Author: erans
Date: Tue Apr 15 12:22:31 2014
New Revision: 1587548

URL: http://svn.apache.org/r1587548
Log:
MATH-1118
Equality of double values in accordance with the semantics of "java.lang.Double".

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

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathUtils.java?rev=1587548&r1=1587547&r2=1587548&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathUtils.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathUtils.java Tue Apr 15 12:22:31 2014
@@ -58,6 +58,18 @@ public final class MathUtils {
     }
 
     /**
+     * Returns {@code true} if the values are equal according to semantics of
+     * {@link Double#equals(Object)}.
+     *
+     * @param x Value
+     * @param y Value
+     * @return {@code new Double(x).equals(new Double(y))}
+     */
+    public static boolean equals(double x, double y) {
+        return new Double(x).equals(new Double(y));
+    }
+
+    /**
      * Returns an integer hash code representing the given double array.
      *
      * @param value the value to be hashed (may be null)

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathUtilsTest.java?rev=1587548&r1=1587547&r2=1587548&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathUtilsTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathUtilsTest.java Tue Apr 15 12:22:31 2014
@@ -25,11 +25,28 @@ import org.junit.Test;
 
 /**
  * Test cases for the MathUtils class.
+ *
  * @version $Id$
- *          2007) $
  */
 public final class MathUtilsTest {
     @Test
+    public void testEqualsDouble() {
+        final double x = 1234.5678;
+        Assert.assertTrue(MathUtils.equals(x, x));
+        Assert.assertFalse(MathUtils.equals(x, -x));
+
+        // Special cases (cf. semantics of JDK's "Double").
+        // 1. NaN
+        Assert.assertTrue(MathUtils.equals(Double.NaN, Double.NaN));
+        // 2. Negative zero
+        final double mZero = -0d;
+        final double zero = 0d;
+        Assert.assertTrue(MathUtils.equals(zero, zero));
+        Assert.assertTrue(MathUtils.equals(mZero, mZero));
+        Assert.assertFalse(MathUtils.equals(mZero, zero));
+    }
+
+    @Test
     public void testHash() {
         double[] testArray = {
             Double.NaN,