From b1bbf2ef8412baea127c23f5fa029ffe397c552d Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Thu, 18 Sep 2014 11:10:23 +0200 Subject: [PATCH] add relational operators --- DataStructures/FixedPointNumber.h | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/DataStructures/FixedPointNumber.h b/DataStructures/FixedPointNumber.h index 45655fd1f..77de365aa 100644 --- a/DataStructures/FixedPointNumber.h +++ b/DataStructures/FixedPointNumber.h @@ -101,7 +101,7 @@ class FixedPointNumber // cast to floating point type T, return value template ::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 return static_cast(static_cast(m_fixed_point_state)) / PRECISION; @@ -116,11 +116,22 @@ class FixedPointNumber } // 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; } + // 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 FixedPointNumber operator+(const FixedPointNumber &other) const noexcept { @@ -170,7 +181,10 @@ class FixedPointNumber temp *= other.m_fixed_point_state; // rounding! - temp = temp + ((temp & 1 << (FractionalBitSize - 1)) << 1); + if (!truncate_results) + { + temp = temp + ((temp & 1 << (FractionalBitSize - 1)) << 1); + } temp >>= FractionalBitSize; this->m_fixed_point_state = static_cast(temp); return *this;