Loading...
Searching...
No Matches
fp_compare_epsilon.ipp
Go to the documentation of this file.
//
// Copyright 2014 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
#include <cmath>
#include <typeinfo>
#pragma once
namespace uhd { namespace math { namespace fp_compare {
template <>
UHD_INLINE fp_compare_epsilon<float>::fp_compare_epsilon(float value)
{
_value = value;
_epsilon = SINGLE_PRECISION_EPSILON;
}
template <>
UHD_INLINE fp_compare_epsilon<double>::fp_compare_epsilon(double value)
{
_value = value;
_epsilon = DOUBLE_PRECISION_EPSILON;
}
template <typename float_t>
UHD_INLINE fp_compare_epsilon<float_t>::fp_compare_epsilon(float_t value, float_t epsilon)
: _value(value), _epsilon(epsilon)
{ /* NOP */
}
template <typename float_t>
UHD_INLINE fp_compare_epsilon<float_t>::fp_compare_epsilon(
const fp_compare_epsilon<float_t>& copy)
: _value(copy._value), _epsilon(copy._epsilon)
{ /* NOP */
}
template <typename float_t>
UHD_INLINE fp_compare_epsilon<float_t>::~fp_compare_epsilon()
{ /* NOP */
}
template <typename float_t>
UHD_INLINE void fp_compare_epsilon<float_t>::operator=(
const fp_compare_epsilon<float_t>& copy)
{
_value = copy._value;
_epsilon = copy._epsilon;
}
template <typename float_t>
UHD_INLINE bool operator==(
fp_compare_epsilon<float_t> lhs, fp_compare_epsilon<float_t> rhs)
{
// If raw bit values are equal, then they're equal. This also catches
// the case where both are 0.0!
if (lhs._value == rhs._value) {
return true;
}
// If one of them is within epsilon of zero, but the other is not, then
// they're also not equal.
if ((std::abs(lhs._value) <= lhs._epsilon && std::abs(rhs._value) > rhs._epsilon)
|| (std::abs(lhs._value) > lhs._epsilon
&& std::abs(rhs._value) <= rhs._epsilon)) {
return false;
}
// In all other cases, we use the "close enough with tolerance epsilon"
// algorithm as described in math.hpp.
const bool lhs_compare =
((std::abs(lhs._value - rhs._value) / std::abs(lhs._value)) <= lhs._epsilon);
const bool rhs_compare =
((std::abs(lhs._value - rhs._value) / std::abs(rhs._value)) <= rhs._epsilon);
return (lhs_compare || rhs_compare);
}
template <typename float_t>
UHD_INLINE bool operator!=(
fp_compare_epsilon<float_t> lhs, fp_compare_epsilon<float_t> rhs)
{
return !(lhs == rhs);
}
template <typename float_t>
UHD_INLINE bool operator<(
fp_compare_epsilon<float_t> lhs, fp_compare_epsilon<float_t> rhs)
{
return (lhs != rhs) && (lhs._value < rhs._value);
}
template <typename float_t>
UHD_INLINE bool operator<=(
fp_compare_epsilon<float_t> lhs, fp_compare_epsilon<float_t> rhs)
{
return !(lhs > rhs);
}
template <typename float_t>
UHD_INLINE bool operator>(
fp_compare_epsilon<float_t> lhs, fp_compare_epsilon<float_t> rhs)
{
return (lhs != rhs) && (lhs._value > rhs._value);
}
template <typename float_t>
UHD_INLINE bool operator>=(
fp_compare_epsilon<float_t> lhs, fp_compare_epsilon<float_t> rhs)
{
return !(lhs < rhs);
}
template <typename float_t>
UHD_INLINE bool operator==(fp_compare_epsilon<float_t> lhs, double rhs)
{
return lhs == fp_compare_epsilon<float_t>(static_cast<float_t>(rhs));
}
template <typename float_t>
UHD_INLINE bool operator!=(fp_compare_epsilon<float_t> lhs, double rhs)
{
return !(lhs == rhs);
}
template <typename float_t>
UHD_INLINE bool operator<(fp_compare_epsilon<float_t> lhs, double rhs)
{
return (lhs != rhs) && (lhs._value < rhs);
}
template <typename float_t>
UHD_INLINE bool operator<=(fp_compare_epsilon<float_t> lhs, double rhs)
{
return !(lhs > rhs);
}
template <typename float_t>
UHD_INLINE bool operator>(fp_compare_epsilon<float_t> lhs, double rhs)
{
return (lhs != rhs) && (lhs._value > rhs);
}
template <typename float_t>
UHD_INLINE bool operator>=(fp_compare_epsilon<float_t> lhs, double rhs)
{
return !(lhs < rhs);
}
template <typename float_t>
UHD_INLINE bool operator==(double lhs, fp_compare_epsilon<float_t> rhs)
{
return fp_compare_epsilon<float_t>(static_cast<float_t>(lhs)) == rhs;
}
template <typename float_t>
UHD_INLINE bool operator!=(double lhs, fp_compare_epsilon<float_t> rhs)
{
return !(lhs == rhs);
}
template <typename float_t>
UHD_INLINE bool operator<(double lhs, fp_compare_epsilon<float_t> rhs)
{
return (lhs != rhs) && (lhs < rhs._value);
}
template <typename float_t>
UHD_INLINE bool operator<=(double lhs, fp_compare_epsilon<float_t> rhs)
{
return !(lhs > rhs);
}
template <typename float_t>
UHD_INLINE bool operator>(double lhs, fp_compare_epsilon<float_t> rhs)
{
return (lhs != rhs) && (lhs > rhs._value);
}
template <typename float_t>
UHD_INLINE bool operator>=(double lhs, fp_compare_epsilon<float_t> rhs)
{
return !(lhs < rhs);
}
}}} // namespace uhd::math::fp_compare
uhd::math::fp_compare::fp_compare_epsilon
Definition math.hpp:70
uhd::math::fp_compare::fp_compare_epsilon< double >::_epsilon
double _epsilon
Definition math.hpp:79
uhd::math::fp_compare::fp_compare_epsilon::~fp_compare_epsilon
UHD_INLINE ~fp_compare_epsilon()
Definition fp_compare_epsilon.ipp:46
uhd::math::fp_compare::fp_compare_epsilon::operator=
UHD_INLINE void operator=(const fp_compare_epsilon &copy)
Definition fp_compare_epsilon.ipp:51
uhd::math::fp_compare::fp_compare_epsilon< double >::_value
double _value
Definition math.hpp:78
uhd::math::fp_compare::fp_compare_epsilon::fp_compare_epsilon
UHD_INLINE fp_compare_epsilon(float_t value)
UHD_INLINE
#define UHD_INLINE
Definition config.h:65
uhd::math::fp_compare
Definition fp_compare_delta.ipp:15
uhd::math::fp_compare::operator!=
UHD_INLINE bool operator!=(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition fp_compare_delta.ipp:71
uhd::math::fp_compare::operator<=
UHD_INLINE bool operator<=(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition fp_compare_delta.ipp:84
uhd::math::fp_compare::operator==
UHD_INLINE bool operator==(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition fp_compare_delta.ipp:64
uhd::math::fp_compare::operator<
UHD_INLINE bool operator<(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition fp_compare_delta.ipp:77
uhd::math::fp_compare::operator>
UHD_INLINE bool operator>(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition fp_compare_delta.ipp:90
uhd::math::fp_compare::operator>=
UHD_INLINE bool operator>=(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition fp_compare_delta.ipp:97
uhd::math
Definition fp_compare_delta.ipp:15
uhd
Definition build_info.hpp:12