nearEqual function

bool nearEqual (double a, double b, double epsilon)

Whether two doubles are within a given distance of each other.

The epsilon argument must be positive and not null. The a and b arguments may be null. A null value is only considered near-equal to another null value.

Implementation

bool nearEqual(double a, double b, double epsilon) {
  assert(epsilon != null);
  assert(epsilon >= 0.0);
  if (a == null || b == null)
    return a == b;
  return (a > (b - epsilon)) && (a < (b + epsilon)) || a == b;
}