fixed an error in SparseRealVector.isInfinite, NaN was not checked beforehand

fixed an error in SparseRealVector.hashcode, code did not depend on vector entries
fixed tests accordingly

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@766337 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2009-04-18 15:17:12 +00:00
parent 11e18f48ff
commit 9b4adf4df9
2 changed files with 17 additions and 5 deletions

View File

@ -593,14 +593,20 @@ public class SparseRealVector implements RealVector {
/** {@inheritDoc} */
public boolean isInfinite() {
boolean infiniteFound = false;
boolean nanFound = false;
Iterator iter = entries.iterator();
while (iter.hasNext()) {
iter.advance();
if (Double.isInfinite(iter.value())) {
return true;
final double value = iter.value();
if (Double.isNaN(value)) {
nanFound = true;
}
if (Double.isInfinite(value)) {
infiniteFound = true;
}
}
return false;
return infiniteFound && (!nanFound);
}
/** {@inheritDoc} */
@ -1228,6 +1234,12 @@ public class SparseRealVector implements RealVector {
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;
}

View File

@ -1082,7 +1082,7 @@ public class SparseRealVectorTest extends TestCase {
assertFalse(v.isInfinite());
v.setEntry(0, Double.POSITIVE_INFINITY);
assertFalse(v.isInfinite()); // NaN is checked before infinity
assertFalse(v.isInfinite()); // NaN has higher priority than infinity
v.setEntry(1, 1);
assertTrue(v.isInfinite());
@ -1091,7 +1091,7 @@ public class SparseRealVectorTest extends TestCase {
assertNotSame(v, new SparseRealVector(new double[] { 0, 1, 2 + Math.ulp(2)}));
assertNotSame(v, new SparseRealVector(new double[] { 0, 1, 2, 3 }));
assertEquals(new SparseRealVector(new double[] { Double.NaN, 1, 2 }).hashCode(),
assertTrue(new SparseRealVector(new double[] { Double.NaN, 1, 2 }).hashCode() !=
new SparseRealVector(new double[] { 0, Double.NaN, 2 }).hashCode());
assertTrue(new SparseRealVector(new double[] { Double.NaN, 1, 2 }).hashCode() !=