commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From billbar...@apache.org
Subject svn commit: r767782 - /commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealVector.java
Date Thu, 23 Apr 2009 04:09:28 GMT
Author: billbarker
Date: Thu Apr 23 04:09:27 2009
New Revision: 767782

URL: http://svn.apache.org/viewvc?rev=767782&view=rev
Log:
Fix equals and hashcode to do exact comparisons

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealVector.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealVector.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealVector.java?rev=767782&r1=767781&r2=767782&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealVector.java
(original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/linear/SparseRealVector.java
Thu Apr 23 04:09:27 2009
@@ -1224,7 +1224,11 @@
         return getData();
     }
 
-    /** {@inheritDoc} */
+    /** {@inheritDoc} 
+     * <p> Implementation Note: This works on exact values, and as a result
+     * it is possible for {@code a.subtract(b)} to be the zero vector, while
+     * {@code a.hashCode() != b.hashCode()}.</p>
+     */
     @Override
     public int hashCode() {
         final int prime = 31;
@@ -1233,10 +1237,21 @@
         temp = Double.doubleToLongBits(epsilon);
         result = prime * result + (int) (temp ^ (temp >>> 32));
         result = prime * result + virtualSize;
+        Iterator iter = entries.iterator();
+        while (iter.hasNext()) {
+            iter.advance();
+            temp = Double.doubleToLongBits(iter.value());
+            result = prime * result + (int) (temp ^ (temp >>32));
+        }
         return result;
     }
 
-    /** {@inheritDoc} */
+    /**  
+     * <p> Implementation Note: This performs an exact comparison, and as a result
+     * it is possible for {@code a.subtract(b}} to be the zero vector, while 
+     * {@code  a.equals(b) == false}.</p>
+     * {@inheritDoc}
+     */
     @Override
     public boolean equals(Object obj) {
         if (this == obj) {
@@ -1259,16 +1274,16 @@
         Iterator iter = entries.iterator();
         while (iter.hasNext()) {
             iter.advance();
-            double test = iter.value() - other.getEntry(iter.key());
-            if (Math.abs(test) > epsilon) {
+            double test = other.getEntry(iter.key());
+            if (Double.doubleToLongBits(test) != Double.doubleToLongBits(iter.value())) {
                 return false;
             }
         }
         iter = other.getEntries().iterator();
         while (iter.hasNext()) {
             iter.advance();
-            double test = iter.value() - getEntry(iter.key());
-            if (!isZero(test)) {
+            double test = iter.value();
+            if (Double.doubleToLongBits(test) != Double.doubleToLongBits(iter.value())) {
                 return false;
             }
         }



Mime
View raw message