Better float comparison

This commit is contained in:
pahimar 2013-09-10 13:29:30 -04:00
parent 420474b900
commit c532feff42

View file

@ -75,7 +75,9 @@ public class EmcValue implements Comparable<EmcValue> {
EmcValue emcValue = (EmcValue) object;
return ((value == emcValue.value) && (recoveryPercent == emcValue.recoveryPercent) && (components.equals(emcValue.getComponents())));
return (Float.compare(this.value, emcValue.value) == 0) &&
(Float.compare(this.recoveryPercent, emcValue.recoveryPercent) == 0) &&
components.equals(emcValue.getComponents());
}
@Override
@ -121,16 +123,21 @@ public class EmcValue implements Comparable<EmcValue> {
@Override
public int compareTo(EmcValue emcValue) {
if (Float.compare(this.value, emcValue.value) == 0) {
if (Float.compare(this.recoveryPercent, emcValue.recoveryPercent) == 0) {
return (this.components.hashCode() - emcValue.components.hashCode());
if (emcValue instanceof EmcValue) {
if (Float.compare(this.value, emcValue.value) == 0) {
if (Float.compare(this.recoveryPercent, emcValue.recoveryPercent) == 0) {
return (this.components.hashCode() - emcValue.components.hashCode());
}
else {
return Float.compare(this.recoveryPercent, emcValue.recoveryPercent);
}
}
else {
return Float.compare(this.recoveryPercent, emcValue.recoveryPercent);
return Float.compare(this.value, emcValue.value);
}
}
else {
return Float.compare(this.value, emcValue.value);
return 1;
}
}