mirror of
https://github.com/apache/commons-math.git
synced 2025-02-07 02:29:20 +00:00
Fix equals and hashcode to do exact comparisons
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@767782 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
7d6a1cec1a
commit
e1452ca6ea
@ -1224,7 +1224,11 @@ public class SparseRealVector implements RealVector {
|
|||||||
return getData();
|
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
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
@ -1233,10 +1237,21 @@ public class SparseRealVector implements RealVector {
|
|||||||
temp = Double.doubleToLongBits(epsilon);
|
temp = Double.doubleToLongBits(epsilon);
|
||||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||||
result = prime * result + virtualSize;
|
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;
|
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
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (this == obj) {
|
if (this == obj) {
|
||||||
@ -1259,16 +1274,16 @@ public class SparseRealVector implements RealVector {
|
|||||||
Iterator iter = entries.iterator();
|
Iterator iter = entries.iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
iter.advance();
|
iter.advance();
|
||||||
double test = iter.value() - other.getEntry(iter.key());
|
double test = other.getEntry(iter.key());
|
||||||
if (Math.abs(test) > epsilon) {
|
if (Double.doubleToLongBits(test) != Double.doubleToLongBits(iter.value())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
iter = other.getEntries().iterator();
|
iter = other.getEntries().iterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
iter.advance();
|
iter.advance();
|
||||||
double test = iter.value() - getEntry(iter.key());
|
double test = iter.value();
|
||||||
if (!isZero(test)) {
|
if (Double.doubleToLongBits(test) != Double.doubleToLongBits(iter.value())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user