add relational operators

This commit is contained in:
Dennis Luxen 2014-09-18 11:10:23 +02:00
parent 201062cade
commit b1bbf2ef84

View File

@ -101,7 +101,7 @@ class FixedPointNumber
// cast to floating point type T, return value // cast to floating point type T, return value
template <typename T, template <typename T,
typename std::enable_if<std::is_floating_point<T>::value>::type * = nullptr> typename std::enable_if<std::is_floating_point<T>::value>::type * = nullptr>
explicit operator T() const noexcept explicit operator const T() const noexcept
{ {
// casts to external type (signed or unsigned) and then to float // casts to external type (signed or unsigned) and then to float
return static_cast<T>(static_cast<state_signage>(m_fixed_point_state)) / PRECISION; return static_cast<T>(static_cast<state_signage>(m_fixed_point_state)) / PRECISION;
@ -116,11 +116,22 @@ class FixedPointNumber
} }
// compare, ie. sort fixed-point numbers // compare, ie. sort fixed-point numbers
void operator<(const FixedPointNumber &other) const noexcept bool operator<(const FixedPointNumber &other) const noexcept
{ {
return m_fixed_point_state < other.m_fixed_point_state; return m_fixed_point_state < other.m_fixed_point_state;
} }
// equality, ie. sort fixed-point numbers
bool operator==(const FixedPointNumber &other) const noexcept
{
return m_fixed_point_state == other.m_fixed_point_state;
}
bool operator!=(const FixedPointNumber &other) const { return !(*this == other); }
bool operator>(const FixedPointNumber &other) const { return other < *this; }
bool operator<=(const FixedPointNumber &other) const { return !(other < *this); }
bool operator>=(const FixedPointNumber &other) const { return !(*this < other); }
// arithmetic operators // arithmetic operators
FixedPointNumber operator+(const FixedPointNumber &other) const noexcept FixedPointNumber operator+(const FixedPointNumber &other) const noexcept
{ {
@ -170,7 +181,10 @@ class FixedPointNumber
temp *= other.m_fixed_point_state; temp *= other.m_fixed_point_state;
// rounding! // rounding!
temp = temp + ((temp & 1 << (FractionalBitSize - 1)) << 1); if (!truncate_results)
{
temp = temp + ((temp & 1 << (FractionalBitSize - 1)) << 1);
}
temp >>= FractionalBitSize; temp >>= FractionalBitSize;
this->m_fixed_point_state = static_cast<decltype(m_fixed_point_state)>(temp); this->m_fixed_point_state = static_cast<decltype(m_fixed_point_state)>(temp);
return *this; return *this;