refs #197 fixing (hopefully) comparison between null quantities

and related rationalization of PQ comparison.
This commit is contained in:
Mathew Sutcliffe
2014-04-08 16:55:11 +01:00
committed by Klaus Basan
parent 8ba2badb61
commit a4e3ac6f40
5 changed files with 33 additions and 11 deletions

View File

@@ -16,7 +16,7 @@ namespace BlackMisc
* Constructor by double
*/
template <class MU, class PQ> CPhysicalQuantity<MU, PQ>::CPhysicalQuantity(double value, const MU &unit) :
m_value(value), m_unit(unit)
m_value(unit.isNull() ? 0.0 : value), m_unit(unit)
{
// void
}
@@ -37,7 +37,10 @@ namespace BlackMisc
template <class MU, class PQ> bool CPhysicalQuantity<MU, PQ>::operator ==(const CPhysicalQuantity<MU, PQ> &other) const
{
if (this == &other) return true;
if (this->isNull() && other.isNull()) return true; // preliminary fix
if (this->isNull()) return other.isNull();
if (other.isNull()) return false;
double diff = std::abs(this->m_value - other.value(this->m_unit));
return diff <= this->m_unit.getEpsilon();
}
@@ -181,7 +184,8 @@ namespace BlackMisc
*/
template <class MU, class PQ> bool CPhysicalQuantity<MU, PQ>::operator <(const CPhysicalQuantity<MU, PQ> &other) const
{
if ((*this) == other) return false;
if (*this == other) return false;
if (this->isNull() || other.isNull()) return false;
return (this->m_value < other.value(this->m_unit));
}
@@ -191,7 +195,6 @@ namespace BlackMisc
*/
template <class MU, class PQ> bool CPhysicalQuantity<MU, PQ>::operator >(const CPhysicalQuantity<MU, PQ> &other) const
{
if (this == &other) return false;
return other < *this;
}
@@ -200,8 +203,8 @@ namespace BlackMisc
*/
template <class MU, class PQ> bool CPhysicalQuantity<MU, PQ>::operator >=(const CPhysicalQuantity<MU, PQ> &other) const
{
if (this == &other) return true;
return !(*this < other);
if (*this == other) return true;
return *this > other;
}
/*
@@ -209,8 +212,8 @@ namespace BlackMisc
*/
template <class MU, class PQ> bool CPhysicalQuantity<MU, PQ>::operator <=(const CPhysicalQuantity<MU, PQ> &other) const
{
if (this == &other) return true;
return !(*this > other);
if (*this == other) return true;
return *this < other;
}
/*
@@ -325,6 +328,9 @@ namespace BlackMisc
{
const auto &other = static_cast<const CPhysicalQuantity &>(otherBase);
if (this->isNull() > other.isNull()) { return -1; }
if (this->isNull() < other.isNull()) { return 1; }
if (*this < other) { return -1; }
else if (*this > other) { return 1; }
else { return 0; }