areWithin function

bool areWithin(
  1. Quantity q1,
  2. Quantity q2,
  3. Quantity tolerance
)

Returns whether or not the magnitude of the difference between two quantities is less than or equal to the specified tolerance.

Implementation

bool areWithin(Quantity q1, Quantity q2, Quantity tolerance) {
  if (q1.dimensions != q2.dimensions || q2.dimensions != tolerance.dimensions) {
    throw DimensionsException('The two quantities and tolerance must have the same dimensions');
  }

  return (q1 - q2).abs().valueSI <= tolerance.valueSI;
}