USRP Hardware Driver and USRP Manual: fp_compare_epsilon.ipp Source File

| USRP Hardware Driver and USRP Manual Version: 4.8.0.makepkg-0-g308126a4 UHD and USRP Manual |

Loading...
Searching...
No Matches
fp_compare_epsilon.ipp
Go to the documentation of this file.
1//
2// Copyright 2014 Ettus Research LLC
3// Copyright 2018 Ettus Research, a National Instruments Company
4//
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
7
8
9#include <cmath>
10#include <typeinfo>
11
12
13#pragma once
14
15
16namespace uhd { namespace math { namespace fp_compare {
17
18template <>
19 UHD_INLINE fp_compare_epsilon<float>::fp_compare_epsilon(float value)
20{
21 _value = value;
22 _epsilon = SINGLE_PRECISION_EPSILON;
23}
24
25template <>
26 UHD_INLINE fp_compare_epsilon<double>::fp_compare_epsilon(double value)
27{
28 _value = value;
29 _epsilon = DOUBLE_PRECISION_EPSILON;
30}
31
32template <typename float_t>
33 UHD_INLINE fp_compare_epsilon<float_t>::fp_compare_epsilon(float_t value, float_t epsilon)
34 : _value(value), _epsilon(epsilon)
35{ /* NOP */
36}
37
38template <typename float_t>
39 UHD_INLINE fp_compare_epsilon<float_t>::fp_compare_epsilon(
40 const fp_compare_epsilon<float_t>& copy)
41 : _value(copy._value), _epsilon(copy._epsilon)
42{ /* NOP */
43}
44
45template <typename float_t>
46 UHD_INLINE fp_compare_epsilon<float_t>::~fp_compare_epsilon()
47{ /* NOP */
48}
49
50template <typename float_t>
51 UHD_INLINE void fp_compare_epsilon<float_t>::operator=(
52 const fp_compare_epsilon<float_t>& copy)
53{
54 _value = copy._value;
55 _epsilon = copy._epsilon;
56}
57
58template <typename float_t>
59 UHD_INLINE bool operator==(
60 fp_compare_epsilon<float_t> lhs, fp_compare_epsilon<float_t> rhs)
61{
62 // If raw bit values are equal, then they're equal. This also catches
63 // the case where both are 0.0!
64 if (lhs._value == rhs._value) {
65 return true;
66 }
67
68 // If one of them is within epsilon of zero, but the other is not, then
69 // they're also not equal.
70 if ((std::abs(lhs._value) <= lhs._epsilon && std::abs(rhs._value) > rhs._epsilon)
71 || (std::abs(lhs._value) > lhs._epsilon
72 && std::abs(rhs._value) <= rhs._epsilon)) {
73 return false;
74 }
75
76 // In all other cases, we use the "close enough with tolerance epsilon"
77 // algorithm as described in math.hpp.
78 const bool lhs_compare =
79 ((std::abs(lhs._value - rhs._value) / std::abs(lhs._value)) <= lhs._epsilon);
80 const bool rhs_compare =
81 ((std::abs(lhs._value - rhs._value) / std::abs(rhs._value)) <= rhs._epsilon);
82
83 return (lhs_compare || rhs_compare);
84}
85
86 template <typename float_t>
87 UHD_INLINE bool operator!=(
88 fp_compare_epsilon<float_t> lhs, fp_compare_epsilon<float_t> rhs)
89{
90 return !(lhs == rhs);
91}
92
93template <typename float_t>
94 UHD_INLINE bool operator<(
95 fp_compare_epsilon<float_t> lhs, fp_compare_epsilon<float_t> rhs)
96{
97 return (lhs != rhs) && (lhs._value < rhs._value);
98}
99
100template <typename float_t>
101 UHD_INLINE bool operator<=(
102 fp_compare_epsilon<float_t> lhs, fp_compare_epsilon<float_t> rhs)
103{
104 return !(lhs > rhs);
105}
106
107template <typename float_t>
108 UHD_INLINE bool operator>(
109 fp_compare_epsilon<float_t> lhs, fp_compare_epsilon<float_t> rhs)
110{
111 return (lhs != rhs) && (lhs._value > rhs._value);
112}
113
114template <typename float_t>
115 UHD_INLINE bool operator>=(
116 fp_compare_epsilon<float_t> lhs, fp_compare_epsilon<float_t> rhs)
117{
118 return !(lhs < rhs);
119}
120
121template <typename float_t>
122 UHD_INLINE bool operator==(fp_compare_epsilon<float_t> lhs, double rhs)
123{
124 return lhs == fp_compare_epsilon<float_t>(static_cast<float_t>(rhs));
125}
126
127template <typename float_t>
128 UHD_INLINE bool operator!=(fp_compare_epsilon<float_t> lhs, double rhs)
129{
130 return !(lhs == rhs);
131}
132
133template <typename float_t>
134 UHD_INLINE bool operator<(fp_compare_epsilon<float_t> lhs, double rhs)
135{
136 return (lhs != rhs) && (lhs._value < rhs);
137}
138
139template <typename float_t>
140 UHD_INLINE bool operator<=(fp_compare_epsilon<float_t> lhs, double rhs)
141{
142 return !(lhs > rhs);
143}
144
145template <typename float_t>
146 UHD_INLINE bool operator>(fp_compare_epsilon<float_t> lhs, double rhs)
147{
148 return (lhs != rhs) && (lhs._value > rhs);
149}
150
151template <typename float_t>
152 UHD_INLINE bool operator>=(fp_compare_epsilon<float_t> lhs, double rhs)
153{
154 return !(lhs < rhs);
155}
156
157template <typename float_t>
158 UHD_INLINE bool operator==(double lhs, fp_compare_epsilon<float_t> rhs)
159{
160 return fp_compare_epsilon<float_t>(static_cast<float_t>(lhs)) == rhs;
161}
162
163template <typename float_t>
164 UHD_INLINE bool operator!=(double lhs, fp_compare_epsilon<float_t> rhs)
165{
166 return !(lhs == rhs);
167}
168
169template <typename float_t>
170 UHD_INLINE bool operator<(double lhs, fp_compare_epsilon<float_t> rhs)
171{
172 return (lhs != rhs) && (lhs < rhs._value);
173}
174
175template <typename float_t>
176 UHD_INLINE bool operator<=(double lhs, fp_compare_epsilon<float_t> rhs)
177{
178 return !(lhs > rhs);
179}
180
181template <typename float_t>
182 UHD_INLINE bool operator>(double lhs, fp_compare_epsilon<float_t> rhs)
183{
184 return (lhs != rhs) && (lhs > rhs._value);
185}
186
187template <typename float_t>
188 UHD_INLINE bool operator>=(double lhs, fp_compare_epsilon<float_t> rhs)
189{
190 return !(lhs < rhs);
191}
192
193}}} // namespace uhd::math::fp_compare
uhd::math::fp_compare::fp_compare_epsilon
Definition math.hpp:80
uhd::math::fp_compare::fp_compare_epsilon< double >::_epsilon
double _epsilon
Definition math.hpp:89
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:88
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