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