commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject cvs commit: jakarta-commons/math/src/test/org/apache/commons/math/util MathUtilsTest.java
Date Thu, 17 Jun 2004 21:31:01 GMT
psteitz     2004/06/17 14:31:01

  Modified:    math/src/java/org/apache/commons/math/util MathUtils.java
               math/src/test/org/apache/commons/math/util
                        MathUtilsTest.java
  Log:
  Added equals and hash methods for comparing / hashing double values.
  
  Revision  Changes    Path
  1.18      +24 -1     jakarta-commons/math/src/java/org/apache/commons/math/util/MathUtils.java
  
  Index: MathUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/util/MathUtils.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- MathUtils.java	19 May 2004 14:16:32 -0000	1.17
  +++ MathUtils.java	17 Jun 2004 21:31:00 -0000	1.18
  @@ -443,4 +443,27 @@
       public static double sinh(double x) {
           return (Math.exp(x) - Math.exp(-x)) / 2.0;
       }
  +    
  +    /**
  +     * Returns an integer hash code representing the given double value.
  +     *
  +     * @param value  the value to be hashed
  +     * @return the hash code
  +     */
  +    public static int hash(double value) {
  +        long bits = Double.doubleToLongBits(value);
  +        return (int)(bits ^ (bits >>> 32));
  +    }
  +    
  +    /**
  +     * Returns true iff both arguments are NaN or
  +     * neither is NaN and they are equal
  +     *
  +     * @param x first value
  +     * @param y second value
  +     * @return true if the values are equal or both are NaN
  +     */
  +    public static boolean equals(double x, double y) {
  +        return ((Double.isNaN(x) && Double.isNaN(y)) || x == y);
  +    }
   }
  
  
  
  1.14      +35 -2     jakarta-commons/math/src/test/org/apache/commons/math/util/MathUtilsTest.java
  
  Index: MathUtilsTest.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/test/org/apache/commons/math/util/MathUtilsTest.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- MathUtilsTest.java	9 May 2004 04:36:09 -0000	1.13
  +++ MathUtilsTest.java	17 Jun 2004 21:31:00 -0000	1.14
  @@ -353,5 +353,38 @@
       
       public void testSinhNaN() {
           assertTrue(Double.isNaN(MathUtils.sinh(Double.NaN)));
  -    }   
  +    } 
  +    
  +    public void testEquals() {
  +        double[] testArray = {Double.NaN, Double.POSITIVE_INFINITY, 
  +                Double.NEGATIVE_INFINITY, 1d, 0d};
  +        for (int i = 0; i < testArray.length; i++) {
  +            for (int j = 0; j < testArray.length; j ++) {
  +                if (i == j) {
  +                    assertTrue(MathUtils.equals(testArray[i], testArray[j]));
  +                    assertTrue(MathUtils.equals(testArray[j], testArray[i]));
  +                } else {
  +                    assertTrue(!MathUtils.equals(testArray[i], testArray[j]));
  +                    assertTrue(!MathUtils.equals(testArray[j], testArray[i]));
  +                }
  +            }
  +        } 
  +    }
  +    
  +    public void testHash() {
  +        double[] testArray = {Double.NaN, Double.POSITIVE_INFINITY, 
  +                Double.NEGATIVE_INFINITY, 1d, 0d, 1E-14, (1 + 1E-14), 
  +                Double.MIN_VALUE, Double.MAX_VALUE};
  +        for (int i = 0; i < testArray.length; i++) {
  +            for (int j = 0; j < testArray.length; j ++) {
  +                if (i == j) {
  +                    assertEquals(MathUtils.hash(testArray[i]), MathUtils.hash(testArray[j]));
  +                    assertEquals(MathUtils.hash(testArray[j]), MathUtils.hash(testArray[i]));
  +                } else {
  +                    assertTrue(MathUtils.hash(testArray[i]) != MathUtils.hash(testArray[j]));
  +                    assertTrue(MathUtils.hash(testArray[j]) != MathUtils.hash(testArray[i]));
  +                }
  +            }
  +        } 
  +    }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message