Fix floating point comparison

This commit is contained in:
Roland Winklmeier
2018-08-24 14:33:27 +02:00
parent 08e06c6ed1
commit ca9a2e5fd1
4 changed files with 6 additions and 6 deletions

View File

@@ -40,7 +40,7 @@ namespace BlackMisc
int compare(double a, double b) int compare(double a, double b)
{ {
if (a == b) return 0; if (qFuzzyCompare(a, b)) return 0;
return a < b ? -1 : 1; return a < b ? -1 : 1;
} }

View File

@@ -48,7 +48,7 @@ namespace BlackMisc
double CMeasurementUnit::roundToEpsilon(double value) const double CMeasurementUnit::roundToEpsilon(double value) const
{ {
if (this->getEpsilon() == 0 || this->isNull()) { return value; } if (qFuzzyIsNull(getEpsilon()) || this->isNull()) { return value; }
return CMathUtils::roundEpsilon(value, this->getEpsilon()); return CMathUtils::roundEpsilon(value, this->getEpsilon());
} }

View File

@@ -333,7 +333,7 @@ namespace BlackMisc
bool isEpsilon(double value) const bool isEpsilon(double value) const
{ {
if (this->isNull()) return false; if (this->isNull()) return false;
if (value == 0) return true; if (qFuzzyIsNull(value)) return true;
return std::abs(value) <= this->m_data->m_epsilon; return std::abs(value) <= this->m_data->m_epsilon;
} }

View File

@@ -145,7 +145,7 @@ namespace BlackMisc
template <class MU, class PQ> template <class MU, class PQ>
const PQ &CPhysicalQuantity<MU, PQ>::makePositive() const PQ &CPhysicalQuantity<MU, PQ>::makePositive()
{ {
if (this->isNull() || m_value == 0) { return *this->derived(); } if (this->isNull() || qFuzzyIsNull(m_value)) { return *this->derived(); }
if (m_value < 0) { m_value *= -1.0; } if (m_value < 0) { m_value *= -1.0; }
return *this->derived(); return *this->derived();
} }
@@ -153,7 +153,7 @@ namespace BlackMisc
template <class MU, class PQ> template <class MU, class PQ>
const PQ &CPhysicalQuantity<MU, PQ>::makeNegative() const PQ &CPhysicalQuantity<MU, PQ>::makeNegative()
{ {
if (this->isNull() || m_value == 0) { return *this->derived(); } if (this->isNull() || qFuzzyIsNull(m_value)) { return *this->derived(); }
if (m_value > 0) { m_value *= -1.0; } if (m_value > 0) { m_value *= -1.0; }
return *this->derived(); return *this->derived();
} }
@@ -161,7 +161,7 @@ namespace BlackMisc
template<class MU, class PQ> template<class MU, class PQ>
PQ CPhysicalQuantity<MU, PQ>::abs() const PQ CPhysicalQuantity<MU, PQ>::abs() const
{ {
if (this->isNull() || m_value == 0) { return *this->derived(); } if (this->isNull() || qFuzzyIsNull(m_value)) { return *this->derived(); }
if (m_value >= 0) { return *this->derived(); } if (m_value >= 0) { return *this->derived(); }
PQ copy(*this->derived()); PQ copy(*this->derived());
return copy.makePositive(); return copy.makePositive();