operator + method

Quantity operator +(
  1. dynamic addend
)

Returns the sum of this Quantity and addend.

  • If an attempt is made to add two Quantity objects having different dimensions, this method 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 sum 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](http://physics.nist.gov/cuu/Uncertainty/combination.html))

Implementation

Quantity operator +(dynamic addend) {
  if (addend == null) throw const QuantityException('Cannot add NULL to Quantity');

  // Scalars allow addition of numbers (the standard uncertainty remains the same).
  if (isScalar && (addend is num || addend is Number)) return this + Scalar(value: addend);

  // Every other Quantity type can only add another Quantity.
  if (addend is! Quantity) {
    throw const QuantityException('Cannot add a anything other than a Quantity to a non-Scalar Quantity');
  }
  final q2 = addend;
  if (dimensions != q2.dimensions) {
    throw DimensionsException('Can\'t add Quantities having different dimensions:  $dimensions and ${q2.dimensions}');
  }

  // Calculate the uncertainty, if necessary.
  final newValueSI = valueSI + q2.valueSI;
  final sumUr = calcRelativeCombinedUncertaintySumDiff(this, addend, newValueSI);

  if (dynamicQuantityTyping) {
    return dimensions.toQuantity(newValueSI, null, sumUr);
  } else {
    return MiscQuantity(newValueSI, dimensions, sumUr);
  }
}