isWithinTolerance method

bool isWithinTolerance(
  1. Rational target,
  2. Rational tolerance
)

Checks if this Rational is within a fixed tolerance of the target value.

Formula: (this - target).abs() <= tolerance. Assumes tolerance is a non-negative Rational.

Implementation

bool isWithinTolerance(Rational target, Rational tolerance) {
  assert(tolerance >= Rational.zero, 'Tolerance cannot be negative');
  return (this - target).abs() <= tolerance;
}