USRP Hardware Driver and USRP Manual Version: 3.15.0.0-MacPorts-Release
UHD and USRP Manual
 
Loading...
Searching...
No Matches
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"
18namespace _bmint = boost::integer;
19#else
20# include <boost/math/common_factor.hpp>
21namespace _bmint = boost::math;
22#endif
23
24
25namespace uhd {
26
31namespace math {
32
49static const float SINGLE_PRECISION_EPSILON = 1.19e-7f;
50static const double DOUBLE_PRECISION_EPSILON = 2.22e-16;
51
52namespace fp_compare {
53
67template <typename float_t> class fp_compare_epsilon
68{
69public:
71 UHD_INLINE fp_compare_epsilon(float_t value, float_t epsilon);
74 UHD_INLINE void operator=(const fp_compare_epsilon& copy);
75
76 float_t _value;
77 float_t _epsilon;
78};
79
80/* A Note on Floating Point Equality with Epsilons
81 *
82 * There are obviously a lot of strategies for defining floating point
83 * equality, and in the end it all comes down to the domain at hand. UHD's
84 * floating-point-with-epsilon comparison algorithm is based on the method
85 * presented in Knuth's "The Art of Computer Science" called "very close
86 * with tolerance epsilon".
87 *
88 * [(|u - v| / |u|) <= e] && [(|u - v| / |v|) <= e]
89 *
90 * UHD's modification to this algorithm is using the denominator's epsilon
91 * value (since each float_t object has its own epsilon) for each
92 * comparison.
93 */
94
95template <typename float_t>
98template <typename float_t>
101template <typename float_t>
104template <typename float_t>
107template <typename float_t>
110template <typename float_t>
113
114/* If these operators are used with floats, we rely on type promotion to
115 * double. */
116template <typename float_t>
118template <typename float_t>
120template <typename float_t>
122template <typename float_t>
124template <typename float_t>
126template <typename float_t>
128
129template <typename float_t>
131template <typename float_t>
133template <typename float_t>
135template <typename float_t>
137template <typename float_t>
139template <typename float_t>
141
142} // namespace fp_compare
143
144
151static const float SINGLE_PRECISION_DELTA = 1e-3f;
152static const double DOUBLE_PRECISION_DELTA = 1e-5;
153
155static const double FREQ_COMPARISON_DELTA_HZ = 0.1;
156
157
158namespace fp_compare {
159
173template <typename float_t> class fp_compare_delta
174{
175public:
177 UHD_INLINE fp_compare_delta(float_t value, float_t delta);
180 UHD_INLINE void operator=(const fp_compare_delta& copy);
181
182 float_t _value;
183 float_t _delta;
184};
185
186template <typename float_t>
188template <typename float_t>
190template <typename float_t>
192template <typename float_t>
194template <typename float_t>
196template <typename float_t>
198
199/* If these operators are used with floats, we rely on type promotion to
200 * double. */
201template <typename float_t>
203template <typename float_t>
205template <typename float_t>
207template <typename float_t>
209template <typename float_t>
211template <typename float_t>
213
214template <typename float_t>
216template <typename float_t>
218template <typename float_t>
220template <typename float_t>
222template <typename float_t>
224template <typename float_t>
226
227} // namespace fp_compare
228
229UHD_INLINE bool frequencies_are_equal(double lhs, double rhs)
230{
231 return (fp_compare::fp_compare_delta<double>(lhs, FREQ_COMPARISON_DELTA_HZ)
232 == fp_compare::fp_compare_delta<double>(rhs, FREQ_COMPARISON_DELTA_HZ));
233}
234
236template <typename IntegerType> inline IntegerType lcm(IntegerType x, IntegerType y)
237{
238 // Note: _bmint is defined conditionally at the top of the file
239 return _bmint::lcm<IntegerType>(x, y);
240}
241
243template <typename IntegerType> inline IntegerType gcd(IntegerType x, IntegerType y)
244{
245 // Note: _bmint is defined conditionally at the top of the file
246 return _bmint::gcd<IntegerType>(x, y);
247}
248
249} // namespace math
250} // namespace uhd
251
254
255#endif /* INCLUDED_UHD_UTILS_MATH_HPP */
UHD_INLINE fp_compare_delta(float_t value)
float_t _delta
Definition math.hpp:183
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:182
float_t _epsilon
Definition math.hpp:77
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:76
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:236
UHD_INLINE bool frequencies_are_equal(double lhs, double rhs)
Definition math.hpp:229
IntegerType gcd(IntegerType x, IntegerType y)
Portable version of gcd() across Boost versions.
Definition math.hpp:243
Definition build_info.hpp:13