6#include "structure/math/spk_constants.hpp"
16 template <
typename TType,
bool IsFloat = std::is_
floating_po
int_v<TType>>
33 template <
typename TType>
36 static_assert(std::is_arithmetic_v<TType>,
"TType must be arithmetic");
72 template <
typename TType>
75 static_assert(std::is_floating_point_v<TType>,
"TType must be floating");
80 TType
epsilon{
static_cast<TType
>(spk::Math::Constants::pointPrecision)};
107 SafeComparand(T) -> SafeComparand<std::remove_cv_t<std::remove_reference_t<T>>, std::is_floating_point_v<std::remove_cv_t<std::remove_reference_t<T>>>>;
109 template <
class T,
class U>
110 SafeComparand(T, U) -> SafeComparand<std::remove_cv_t<std::remove_reference_t<T>>, std::is_floating_point_v<std::remove_cv_t<std::remove_reference_t<T>>>>;
112 template <
typename TLeftHandType,
bool IsLeftHandleFloatingType,
typename TRightHandType>
113 requires(std::is_arithmetic_v<TLeftHandType> && std::is_arithmetic_v<TRightHandType>)
114 constexpr bool operator==(
const SafeComparand<TLeftHandType, IsLeftHandleFloatingType> &p_lhs,
const TRightHandType &p_rhs)
116 using Common = std::common_type_t<TLeftHandType, TRightHandType>;
118 if constexpr (IsLeftHandleFloatingType)
120 const Common dv =
static_cast<Common
>(p_lhs.value) -
static_cast<Common
>(p_rhs);
121 const Common eps =
static_cast<Common
>(p_lhs.epsilon);
122 return std::abs(dv) <= eps;
126 if constexpr (std::is_floating_point_v<TRightHandType>)
128 const Common dv =
static_cast<Common
>(p_lhs.value) -
static_cast<Common
>(p_rhs);
129 const Common eps =
static_cast<Common
>(spk::Math::Constants::pointPrecision);
130 return std::abs(dv) <= eps;
134 return p_lhs.value ==
static_cast<TLeftHandType
>(p_rhs);
139 template <
typename TLeftHandType,
typename TRightHandType,
bool IsRightHandFloatingType>
140 requires(std::is_arithmetic_v<TLeftHandType> && std::is_arithmetic_v<TRightHandType>)
143 return (p_rhs == p_lhs);
146 template <
typename TLeftHandType,
bool IsLeftHandFloatingType,
typename TRightHandType,
bool IsRightHandFloatingType>
147 requires(std::is_arithmetic_v<TLeftHandType> && std::is_arithmetic_v<TRightHandType>)
148 constexpr bool operator==(
151 using Common = std::common_type_t<TLeftHandType, TRightHandType>;
152 if constexpr (IsLeftHandFloatingType || IsRightHandFloatingType)
154 const Common dv =
static_cast<Common
>(p_lhs.value) -
static_cast<Common
>(p_rhs.value);
156 if constexpr (IsLeftHandFloatingType && IsRightHandFloatingType)
158 eps = std::min<Common>(
static_cast<Common
>(p_lhs.epsilon),
static_cast<Common
>(p_rhs.epsilon));
160 else if constexpr (IsLeftHandFloatingType)
162 eps =
static_cast<Common
>(p_lhs.epsilon);
166 eps =
static_cast<Common
>(p_rhs.epsilon);
168 return std::abs(dv) <= eps;
172 return static_cast<Common
>(p_lhs.value) ==
static_cast<Common
>(p_rhs.value);
176 template <
typename TLeftHandType,
bool IsLeftHandFloatingType,
typename TRightHandType>
177 requires(std::is_arithmetic_v<TLeftHandType> && std::is_arithmetic_v<TRightHandType>)
180 return !(p_lhs == p_rhs);
183 template <
typename TLeftHandType,
typename TRightHandType,
bool IsRightHandFloatingType>
184 requires(std::is_arithmetic_v<TLeftHandType> && std::is_arithmetic_v<TRightHandType>)
187 return !(p_lhs == p_rhs);
190 template <
typename TLeftHandType,
bool IsLeftHandFloatingType,
typename TRightHandType,
bool IsRightHandFloatingType>
191 requires(std::is_arithmetic_v<TLeftHandType> && std::is_arithmetic_v<TRightHandType>)
192 constexpr bool operator!=(
195 return !(p_lhs == p_rhs);
SafeComparand(const TType &p_value)
Wraps a value for tolerant comparisons.
Definition spk_safe_comparand.hpp:52
TType value
Stored numeric value.
Definition spk_safe_comparand.hpp:43
SafeComparand()=default
Default-initializes the stored value.
TType value_type
Exposes the underlying arithmetic type.
Definition spk_safe_comparand.hpp:40
TType epsilon
Comparison tolerance applied when evaluating equality.
Definition spk_safe_comparand.hpp:80
SafeComparand()=default
Default-initializes the value to zero with default epsilon.
SafeComparand(const TType &p_value, const TType &p_epsilon)
Wraps a floating value with a custom tolerance.
Definition spk_safe_comparand.hpp:99
TType value
Stored numeric value.
Definition spk_safe_comparand.hpp:78
SafeComparand(const TType &p_value)
Wraps a floating value using default epsilon.
Definition spk_safe_comparand.hpp:89
Wraps arithmetic values to compare with tolerance for floating-point inputs.
Definition spk_safe_comparand.hpp:17