compareToRelativeI function
Compares two doubles and determines which double is bigger. a < b -> -1; a ~= b (almost equal according to parameter) -> 0; a > b -> +1.
Implementation
int compareToRelativeI(double a, double b, int decimalPlaces) {
// NANs are equal to nothing,
// not even themselves, and thus they're not bigger or
// smaller than anything either
if (a.isNaN || b.isNaN) {
return a.compareTo(b);
}
// If A or B are infinity (positive or negative) then
// only return true if first is smaller
if (a.isInfinite || b.isInfinite) {
return a.compareTo(b);
}
// If the numbers are equal to within the number of decimal places
// then there's technically no difference
if (almostEqualRelativeI(a, b, decimalPlaces)) {
return 0;
}
// The numbers differ by more than the decimal places, so
// we can check the normal way to see if the first is
// larger than the second.
return a.compareTo(b);
}