commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r280031 - in /jakarta/commons/proper/math/branches/MATH_1_1: src/java/org/apache/commons/math/complex/Complex.java src/test/org/apache/commons/math/complex/ComplexTest.java xdocs/changes.xml
Date Sat, 10 Sep 2005 19:20:03 GMT
Author: psteitz
Date: Sat Sep 10 12:19:58 2005
New Revision: 280031

URL: http://svn.apache.org/viewcvs?rev=280031&view=rev
Log:
Implemented hashCode in the Complex class and changed the semantics of
equals to make all instances with real or imaginary part NaN equal.
PR# 36491 (findbugs report)

Modified:
    jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/Complex.java
    jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/complex/ComplexTest.java
    jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml

Modified: jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/Complex.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/Complex.java?rev=280031&r1=280030&r2=280031&view=diff
==============================================================================
--- jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/Complex.java
(original)
+++ jakarta/commons/proper/math/branches/MATH_1_1/src/java/org/apache/commons/math/complex/Complex.java
Sat Sep 10 12:19:58 2005
@@ -17,6 +17,7 @@
 package org.apache.commons.math.complex;
 
 import java.io.Serializable;
+import org.apache.commons.math.util.MathUtils;
 
 /**
  * Representation of a Complex number - a number which has both a 
@@ -33,7 +34,7 @@
     /** The square root of -1. A number representing "0.0 + 1.0i".*/    
     public static final Complex I = new Complex(0.0, 1.0);
     
-    /** A complex number representing "(Double.NaN) + (Double.NaN)i" */
+    /** A complex number analogous to {@link Double.NaN} */
     public static final Complex NaN = new Complex(Double.NaN, Double.NaN);
 
     /** A complex number representing "1.0 + 0.0i" */    
@@ -147,9 +148,16 @@
     }
     
     /**
-     * Test for the equality of two Complex objects.  If both the
-     * real and imaginary parts of two Complex numbers are exactly
-     * the same, the two Complex objects are considered to be equal.
+     * Test for the equality of two Complex objects.
+     * <p>
+     * If both the real and imaginary parts of two Complex numbers
+     * are exactly the same, and neither is <code>Double.NaN</code>, the two
+     * Complex objects are considered to be equal. 
+     * <p>
+     * All <code>NaN</code> values are considered to be equal - i.e, if either
+     * (or both) real and imaginary parts of the complex number are equal
+     * to Double.NaN, the complex number is equal to 
+     * <code>Complex.NaN</code>.
      *
      * @param other Object to test for equality to this
      * @return true if two Complex objects are equal, false if
@@ -164,20 +172,39 @@
             ret = true;
         } else if (other == null) {
             ret = false;
-        } else {
+        } else  {
             try {
                 Complex rhs = (Complex)other;
+                if (rhs.isNaN()) {
+                    ret = this.isNaN();
+                } else {
                 ret = (Double.doubleToRawLongBits(real) ==
                         Double.doubleToRawLongBits(rhs.getReal())) &&
                     (Double.doubleToRawLongBits(imaginary) ==
                         Double.doubleToRawLongBits(rhs.getImaginary())); 
+                }
             } catch (ClassCastException ex) {
                 // ignore exception
                 ret = false;
             }
         }
-        
+      
         return ret;
+    }
+    
+    /**
+     * Get a hashCode for the complex number.
+     * <p>
+     * All NaN values have the same hash code.
+     * 
+     * @return a hash code value for this object
+     */
+    public int hashCode() {
+        if (isNaN()) {
+            return 7;
+        }
+        return 37 * (17 * MathUtils.hash(imaginary) + 
+            MathUtils.hash(real));
     }
 
     /**

Modified: jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/complex/ComplexTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/complex/ComplexTest.java?rev=280031&r1=280030&r2=280031&view=diff
==============================================================================
--- jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/complex/ComplexTest.java
(original)
+++ jakarta/commons/proper/math/branches/MATH_1_1/src/test/org/apache/commons/math/complex/ComplexTest.java
Sat Sep 10 12:19:58 2005
@@ -161,4 +161,28 @@
         Complex y = new Complex(0.0, 0.0 + Double.MIN_VALUE);
         assertFalse(x.equals(y));
     }
+    
+    public void testEqualsNaN() {
+        Complex realNaN = new Complex(Double.NaN, 0.0);
+        Complex imaginaryNaN = new Complex(0.0, Double.NaN);
+        Complex complexNaN = Complex.NaN;
+        assertTrue(realNaN.equals(imaginaryNaN));
+        assertTrue(imaginaryNaN.equals(complexNaN));
+        assertTrue(realNaN.equals(complexNaN));
+        Double d = new Double(Double.NaN);
+        Double d2 = new Double(Double.NaN);
+        assertTrue(d.equals(d2));
+    }
+    
+    public void testHashCode() {
+        Complex x = new Complex(0.0, 0.0);
+        Complex y = new Complex(0.0, 0.0 + Double.MIN_VALUE);
+        assertFalse(x.hashCode()==y.hashCode());
+        y = new Complex(0.0 + Double.MIN_VALUE, 0.0);
+        assertFalse(x.hashCode()==y.hashCode());
+        Complex realNaN = new Complex(Double.NaN, 0.0);
+        Complex imaginaryNaN = new Complex(0.0, Double.NaN);
+        assertEquals(realNaN.hashCode(), imaginaryNaN.hashCode());
+        assertEquals(imaginaryNaN.hashCode(), Complex.NaN.hashCode());
+    }
 }

Modified: jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml?rev=280031&r1=280030&r2=280031&view=diff
==============================================================================
--- jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml (original)
+++ jakarta/commons/proper/math/branches/MATH_1_1/xdocs/changes.xml Sat Sep 10 12:19:58 2005
@@ -45,11 +45,16 @@
        and numerical utilities, and a PRNG pluggability framework making it
        possible to replace the JDK-supplied random number generator in
        commons-math (and elsewhere) with alternative PRNG implementations.">
+      <action dev="psteitz" type="fix" issue="36491">
+       Implemented hashCode in the Complex class and changed the semantics of
+       equals to make all instances with real or imaginary part NaN equal.
+      </action>
       <action dev="psteitz" type="fix" issue="36450" due-to="Keith McDonald">
 		Fixed bin index overflow problem in EmpiricalDistributionImpl.
 	  </action>
 	  <action dev="brentworden" type="fix" issue="36232" due-to="Xiaogang Zhang">
-		Added protection against numerical overflow and underflow in the isBracketing method.
+        Added protection against numerical overflow and underflow in the
+        isBracketing method.
 	  </action>
 	  <action dev="brentworden" type="fix" issue="36300" due-to="Nikhil Gupte">
 		Fixed division by zero error in rounding methods.



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