operator - method

Quantity operator -(
  1. dynamic subtrahend
)

Returns the difference of this Quantity and subtrahend or (this - q2).

Only a Quantity object having the same dimensions as this Quantity object may be subtracted from it.

  • If an attempt is made to subtract a Quantity object having different dimensions from this Quantity object, this operator will throw a DimensionsException.
  • If the uncertainty is calculated it will be equal to the combined standard uncertainty divided by the absolute value of the difference of the quantities. The standard uncertainty is the square root of the sum of the squares of the two quantities' standard uncertainties.

See NIST Reference on Constants, Units, and Uncertainty: Combining uncertainty components.

Implementation

Quantity operator -(dynamic subtrahend) {
  // Null check
  if (subtrahend == null) throw const QuantityException('Cannot subtract NULL from Quantity');

  // Scalars allow subtraction of numbers.
  if (isScalar && (subtrahend is num || subtrahend is Number)) return this - Scalar(value: subtrahend);

  // Every other Quantity type can only subtract another Quantity.
  if (subtrahend is! Quantity) {
    throw QuantityException('Cannot subtract a ${subtrahend.runtimeType} from a non-Scalar Quantity');
  }
  final q2 = subtrahend;
  if (dimensions != q2.dimensions) {
    throw DimensionsException('''Can't subtract Quantities having different
      dimensions:  $dimensions and ${q2.dimensions}''');
  }

  final newValueSI = valueSI - q2.valueSI;
  final diffUr = calcRelativeCombinedUncertaintySumDiff(this, subtrahend, newValueSI);

  if (dynamicQuantityTyping) {
    return dimensions.toQuantity(valueSI - q2.valueSI, null, diffUr);
  } else {
    return MiscQuantity(valueSI - q2.valueSI, dimensions, diffUr);
  }
}