Quantity& Quantity::operator+=(const Quantity& temp) { if (this == &temp) { m_value *= 2.0; return *this; } // copy input reference so we can change its properties Quantity rQuantity(temp); if (isTemperature() && rQuantity.isTemperature()) { if (!isAbsolute() && rQuantity.isAbsolute()) { setAsAbsolute(); } else if (isAbsolute() && !rQuantity.isAbsolute()){ rQuantity.setAsAbsolute(); } } if (m_units != rQuantity.m_units) { LOG_AND_THROW("Cannot add quantities with different units."); } if (scale() != rQuantity.scale()) { Quantity wRQuantity(rQuantity); wRQuantity.setScale(scale().exponent); m_value += wRQuantity.value(); } else { m_value += rQuantity.value(); } return *this; }
OSQuantityVector& OSQuantityVector::operator-=(Quantity rQuantity) { if (isTemperature() && rQuantity.isTemperature()) { if (isAbsolute() && rQuantity.isAbsolute()) { // units must be the same, check that exponent on this is 1 std::vector<std::string> bus = m_units.baseUnits(); assert(bus.size() == 1); if (m_units.baseUnitExponent(bus[0]) == 1) { setAsRelative(); rQuantity.setAsRelative(); } } else if (!isAbsolute() && rQuantity.isAbsolute()) { setAsAbsolute(); } else if (isAbsolute() && !rQuantity.isAbsolute()) { rQuantity.setAsAbsolute(); } } if (units() != rQuantity.units()) { LOG_AND_THROW("Cannot subtract OSQuantityVector and Quantity with different units (" << units() << " and " << rQuantity.units() << ")."); } if (scale() != rQuantity.scale()) { rQuantity.setScale(scale().exponent); } double value = rQuantity.value(); for (unsigned i = 0, n = size(); i < n; ++i) { m_values[i] -= value; } return *this; }
OSQuantityVector& OSQuantityVector::operator+=(Quantity rQuantity) { if (isTemperature() && rQuantity.isTemperature()) { if (!isAbsolute() && rQuantity.isAbsolute()) { setAsAbsolute(); } else if (isAbsolute() && !rQuantity.isAbsolute()) { rQuantity.setAsAbsolute(); } } if (units() != rQuantity.units()) { LOG_AND_THROW("Cannot add OSQuantityVector and Quantity with different units (" << units() << " and " << rQuantity.units() << ")."); } if (scale() != rQuantity.scale()) { rQuantity.setScale(scale().exponent); } double value = rQuantity.value(); for (unsigned i = 0, n = size(); i < n; ++i) { m_values[i] += value; } return *this; }
Quantity& Quantity::operator+=(const Quantity& rQuantity) { if (this == &rQuantity) { m_value *= 2.0; return *this; } if (m_units != rQuantity.m_units) { LOG_AND_THROW("Cannot add quantities with different units."); } if (scale() != rQuantity.scale()) { Quantity wRQuantity(rQuantity); wRQuantity.setScale(scale().exponent); m_value += wRQuantity.value(); } else { m_value += rQuantity.value(); } if (isTemperature() && rQuantity.isTemperature()) { if (!isAbsolute() && rQuantity.isAbsolute()) { setAsAbsolute(); } } return *this; }
OSQuantityVector& OSQuantityVector::operator+=(OSQuantityVector rVector) { if (this == &rVector) { (*this) *= 2.0; return *this; } if (units() != rVector.units()) { LOG_AND_THROW("Cannot add OSQuantityVectors with different units (" << units() << " and " << rVector.units() << ")."); } unsigned n = size(); if (rVector.size() != n) { LOG_AND_THROW("Cannot add vectors of different sizes."); } if (scale() != rVector.scale()) { rVector.setScale(scale().exponent); } DoubleVector rValues = rVector.values(); for (unsigned i = 0; i < n; ++i) { m_values[i] += rValues[i]; } if (isTemperature() && rVector.isTemperature()) { if (!isAbsolute() && rVector.isAbsolute()) { setAsAbsolute(); } } return *this; }
OSQuantityVector& OSQuantityVector::operator-=(OSQuantityVector rVector) { unsigned n = size(); if (this == &rVector) { clear(); resize(n,0.0); return *this; } if (isTemperature() && rVector.isTemperature()) { if (isAbsolute() && rVector.isAbsolute()) { // units must be the same, check that exponent on this is 1 std::vector<std::string> bus = m_units.baseUnits(); assert(bus.size() == 1); if (m_units.baseUnitExponent(bus[0]) == 1) { setAsRelative(); rVector.setAsRelative(); } } else if (!isAbsolute() && rVector.isAbsolute()) { setAsAbsolute(); } else if (isAbsolute() && !rVector.isAbsolute()) { rVector.setAsAbsolute(); } } if (units() != rVector.units()) { LOG_AND_THROW("Cannot subtract OSQuantityVectors with different units (" << units() << " and " << rVector.units() << ")."); } if (rVector.size() != n) { LOG_AND_THROW("Cannot subtract vectors of different sizes."); } if (scale() != rVector.scale()) { rVector.setScale(scale().exponent); } DoubleVector rValues = rVector.values(); for (unsigned i = 0; i < n; ++i) { m_values[i] -= rValues[i]; } return *this; }
Quantity& Quantity::operator-=(const Quantity& temp) { if (this == &temp) { m_value = 0.0; return *this; } // copy input reference so we can change its properties Quantity rQuantity(temp); if (isTemperature() && rQuantity.isTemperature()) { if (isAbsolute() && rQuantity.isAbsolute()) { // units must be the same, check that exponent on this is 1 std::vector<std::string> bus = baseUnits(); OS_ASSERT(bus.size() == 1); if (baseUnitExponent(bus[0]) == 1) { setAsRelative(); rQuantity.setAsRelative(); } } else if (!isAbsolute() && rQuantity.isAbsolute()) { setAsAbsolute(); } else if (isAbsolute() && !rQuantity.isAbsolute()) { rQuantity.setAsAbsolute(); } } if (m_units != rQuantity.m_units) { LOG_AND_THROW("Cannot subtract quantities with different units."); } if (scale() != rQuantity.scale()) { Quantity wRQuantity(rQuantity); wRQuantity.setScale(scale().exponent); m_value -= wRQuantity.value(); } else { m_value -= rQuantity.value(); } return *this; }