compareToRelativeD function

int compareToRelativeD(
  1. double a,
  2. double b,
  3. double maximumError
)

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 compareToRelativeD(double a, double b, double maximumError) {
  // 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 (almostEqualRelativeD(a, b, maximumError)) {
    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);
}