USRP Hardware Driver and USRP Manual  Version: 3.15.0.0-4+b1satnogs1
UHD and USRP Manual
math.hpp
Go to the documentation of this file.
1 //
2 // Copyright 2014-2015 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 #ifndef INCLUDED_UHD_UTILS_MATH_HPP
9 #define INCLUDED_UHD_UTILS_MATH_HPP
10 
11 #include <uhd/config.hpp>
12 #include <stdint.h>
13 #include <boost/numeric/conversion/bounds.hpp>
14 #include <cmath>
15 #if BOOST_VERSION >= 106700
16 # include <boost/integer/common_factor.hpp>
17 // "bmint" for "boost math integer"
18 namespace _bmint = boost::integer;
19 #else
20 # include <boost/math/common_factor.hpp>
21 namespace _bmint = boost::math;
22 #endif
23 
24 
25 namespace uhd {
26 
31 namespace math {
32 
49 static const float SINGLE_PRECISION_EPSILON = 1.19e-7f;
50 static const double DOUBLE_PRECISION_EPSILON = 2.22e-16;
51 
52 namespace fp_compare {
53 
67 template <typename float_t>
69 {
70 public:
72  UHD_INLINE fp_compare_epsilon(float_t value, float_t epsilon);
75  UHD_INLINE void operator=(const fp_compare_epsilon& copy);
76 
77  float_t _value;
78  float_t _epsilon;
79 };
80 
81 /* A Note on Floating Point Equality with Epsilons
82  *
83  * There are obviously a lot of strategies for defining floating point
84  * equality, and in the end it all comes down to the domain at hand. UHD's
85  * floating-point-with-epsilon comparison algorithm is based on the method
86  * presented in Knuth's "The Art of Computer Science" called "very close
87  * with tolerance epsilon".
88  *
89  * [(|u - v| / |u|) <= e] && [(|u - v| / |v|) <= e]
90  *
91  * UHD's modification to this algorithm is using the denominator's epsilon
92  * value (since each float_t object has its own epsilon) for each
93  * comparison.
94  */
95 
96 template <typename float_t>
99 template <typename float_t>
102 template <typename float_t>
103 UHD_INLINE bool operator<(
105 template <typename float_t>
108 template <typename float_t>
109 UHD_INLINE bool operator>(
111 template <typename float_t>
114 
115 /* If these operators are used with floats, we rely on type promotion to
116  * double. */
117 template <typename float_t>
119 template <typename float_t>
121 template <typename float_t>
122 UHD_INLINE bool operator<(fp_compare_epsilon<float_t> lhs, double rhs);
123 template <typename float_t>
125 template <typename float_t>
126 UHD_INLINE bool operator>(fp_compare_epsilon<float_t> lhs, double rhs);
127 template <typename float_t>
129 
130 template <typename float_t>
132 template <typename float_t>
134 template <typename float_t>
135 UHD_INLINE bool operator<(double lhs, fp_compare_epsilon<float_t> rhs);
136 template <typename float_t>
138 template <typename float_t>
139 UHD_INLINE bool operator>(double lhs, fp_compare_epsilon<float_t> rhs);
140 template <typename float_t>
142 
143 } // namespace fp_compare
144 
145 
152 static const float SINGLE_PRECISION_DELTA = 1e-3f;
153 static const double DOUBLE_PRECISION_DELTA = 1e-5;
154 
156 static const double FREQ_COMPARISON_DELTA_HZ = 0.1;
157 
158 
159 namespace fp_compare {
160 
174 template <typename float_t>
176 {
177 public:
178  UHD_INLINE fp_compare_delta(float_t value);
179  UHD_INLINE fp_compare_delta(float_t value, float_t delta);
182  UHD_INLINE void operator=(const fp_compare_delta& copy);
183 
184  float_t _value;
185  float_t _delta;
186 };
187 
188 template <typename float_t>
190 template <typename float_t>
192 template <typename float_t>
194 template <typename float_t>
196 template <typename float_t>
198 template <typename float_t>
200 
201 /* If these operators are used with floats, we rely on type promotion to
202  * double. */
203 template <typename float_t>
204 UHD_INLINE bool operator==(fp_compare_delta<float_t> lhs, double rhs);
205 template <typename float_t>
206 UHD_INLINE bool operator!=(fp_compare_delta<float_t> lhs, double rhs);
207 template <typename float_t>
208 UHD_INLINE bool operator<(fp_compare_delta<float_t> lhs, double rhs);
209 template <typename float_t>
210 UHD_INLINE bool operator<=(fp_compare_delta<float_t> lhs, double rhs);
211 template <typename float_t>
212 UHD_INLINE bool operator>(fp_compare_delta<float_t> lhs, double rhs);
213 template <typename float_t>
214 UHD_INLINE bool operator>=(fp_compare_delta<float_t> lhs, double rhs);
215 
216 template <typename float_t>
217 UHD_INLINE bool operator==(double lhs, fp_compare_delta<float_t> rhs);
218 template <typename float_t>
219 UHD_INLINE bool operator!=(double lhs, fp_compare_delta<float_t> rhs);
220 template <typename float_t>
221 UHD_INLINE bool operator<(double lhs, fp_compare_delta<float_t> rhs);
222 template <typename float_t>
223 UHD_INLINE bool operator<=(double lhs, fp_compare_delta<float_t> rhs);
224 template <typename float_t>
225 UHD_INLINE bool operator>(double lhs, fp_compare_delta<float_t> rhs);
226 template <typename float_t>
227 UHD_INLINE bool operator>=(double lhs, fp_compare_delta<float_t> rhs);
228 
229 } // namespace fp_compare
230 
231 UHD_INLINE bool frequencies_are_equal(double lhs, double rhs)
232 {
233  return (fp_compare::fp_compare_delta<double>(lhs, FREQ_COMPARISON_DELTA_HZ)
234  == fp_compare::fp_compare_delta<double>(rhs, FREQ_COMPARISON_DELTA_HZ));
235 }
236 
238 template <typename IntegerType>
239 inline IntegerType lcm(IntegerType x, IntegerType y)
240 {
241  // Note: _bmint is defined conditionally at the top of the file
242  return _bmint::lcm<IntegerType>(x, y);
243 }
244 
246 template <typename IntegerType>
247 inline IntegerType gcd(IntegerType x, IntegerType y)
248 {
249  // Note: _bmint is defined conditionally at the top of the file
250  return _bmint::gcd<IntegerType>(x, y);
251 }
252 
253 } // namespace math
254 } // namespace uhd
255 
258 
259 #endif /* INCLUDED_UHD_UTILS_MATH_HPP */
UHD_INLINE fp_compare_delta(float_t value)
float_t _delta
Definition: math.hpp:185
UHD_INLINE void operator=(const fp_compare_delta &copy)
Definition: fp_compare_delta.ipp:53
UHD_INLINE ~fp_compare_delta()
Definition: fp_compare_delta.ipp:49
float_t _value
Definition: math.hpp:184
float_t _epsilon
Definition: math.hpp:78
UHD_INLINE ~fp_compare_epsilon()
Definition: fp_compare_epsilon.ipp:44
UHD_INLINE void operator=(const fp_compare_epsilon &copy)
Definition: fp_compare_epsilon.ipp:48
float_t _value
Definition: math.hpp:77
UHD_INLINE fp_compare_epsilon(float_t value)
#define UHD_INLINE
Definition: config.h:53
UHD_INLINE bool operator!=(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition: fp_compare_delta.ipp:65
UHD_INLINE bool operator<=(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition: fp_compare_delta.ipp:76
UHD_INLINE bool operator==(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition: fp_compare_delta.ipp:59
UHD_INLINE bool operator<(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition: fp_compare_delta.ipp:70
UHD_INLINE bool operator>(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition: fp_compare_delta.ipp:81
UHD_INLINE bool operator>=(fp_compare_delta< float_t > lhs, fp_compare_delta< float_t > rhs)
Definition: fp_compare_delta.ipp:87
IntegerType lcm(IntegerType x, IntegerType y)
Portable version of lcm() across Boost versions.
Definition: math.hpp:239
UHD_INLINE bool frequencies_are_equal(double lhs, double rhs)
Definition: math.hpp:231
IntegerType gcd(IntegerType x, IntegerType y)
Portable version of gcd() across Boost versions.
Definition: math.hpp:247
Definition: build_info.hpp:13