compareToNumbersBetween function

int compareToNumbersBetween (double a, double b, int maxNumbersBetween)

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 compareToNumbersBetween(double a, double b, int maxNumbersBetween) {
  // 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 tolerance then
  // there's technically no difference
  if (almostEqualNumbersBetween(a, b, maxNumbersBetween)) {
    return 0;
  }

  return a.compareTo(b);
}