Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
float_math.hpp
Go to the documentation of this file.
1 /*
2  * Author: Sven Gothel <sgothel@jausoft.com>
3  * Copyright (c) 2020 Gothel Software e.K.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef JAU_BASIC_FLOAT_MATH_HPP_
26 #define JAU_BASIC_FLOAT_MATH_HPP_
27 
28 #include <cmath>
29 #include <type_traits>
30 #include <algorithm>
31 
32 namespace jau {
33  /**
34  // *************************************************
35  // *************************************************
36  // *************************************************
37  */
38 
39  /**
40  * Returns true, if both integer point values differ less than the given delta.
41  * @tparam T an integral type
42  * @param a value to compare
43  * @param b value to compare
44  * @param delta the maximum difference both values may differ
45  */
46  template<class T>
47  bool in_range(const T& a, const T& b, const T& delta)
48  {
49  const T diff = std::fabs(a-b);
50  return diff <= delta ||
51  diff < std::numeric_limits<T>::min(); // subnormal limit
52  }
53 
54  /**
55  * Calculates the smallest floating point value approximation
56  * the given type T can represent, the machine epsilon of T.
57  * @tparam T a non integer float type
58  * @return machine epsilon of T
59  */
60  template<class T>
61  typename std::enable_if<!std::numeric_limits<T>::is_integer, T>::type
63  const T one(1);
64  const T two(2);
65  T x = one, res;
66  do {
67  res = x;
68  } while (one + (x /= two) > one);
69  return res;
70  }
71 
72  /**
73  * Returns true, if both floating point values are equal
74  * in the sense that their potential difference is less or equal <code>epsilon * ulp</code>.
75  * @tparam T a non integer float type
76  * @param a value to compare
77  * @param b value to compare
78  * @param ulp desired precision in ULPs (units in the last place), defaults to 1
79  * @param epsilon the machine epsilon of type T, defaults to <code>std::numeric_limits<T>::epsilon()</code>
80  */
81  template<class T>
82  typename std::enable_if<!std::numeric_limits<T>::is_integer, bool>::type
83  machine_equal(const T& a, const T& b, int ulp=1, const T& epsilon=std::numeric_limits<T>::epsilon())
84  {
85  const T diff = std::fabs(a-b);
86  return diff <= epsilon * ulp ||
87  diff < std::numeric_limits<T>::min(); // subnormal limit
88  }
89 
90  /**
91  * Returns true, if both floating point values are equal
92  * in the sense that their potential difference is less or equal <code>epsilon * |a+b| * ulp</code>,
93  * where <code>|a+b|</code> scales epsilon to the magnitude of used values.
94  * @tparam T a non integer float type
95  * @param a value to compare
96  * @param b value to compare
97  * @param ulp desired precision in ULPs (units in the last place), defaults to 1
98  * @param epsilon the machine epsilon of type T, defaults to <code>std::numeric_limits<T>::epsilon()</code>
99  */
100  template<class T>
101  typename std::enable_if<!std::numeric_limits<T>::is_integer, bool>::type
102  almost_equal(const T& a, const T& b, int ulp=1, const T& epsilon=std::numeric_limits<T>::epsilon())
103  {
104  const T diff = std::fabs(a-b);
105  return diff <= epsilon * std::fabs(a+b) * ulp ||
106  diff < std::numeric_limits<T>::min(); // subnormal limit
107  }
108 
109 } // namespace jau
110 
111 #endif /* JAU_BASIC_FLOAT_MATH_HPP_ */
jau
Definition: basic_algos.hpp:34
jau::in_range
bool in_range(const T &a, const T &b, const T &delta)
Returns true, if both integer point values differ less than the given delta.
Definition: float_math.hpp:47
jau::machineEpsilon
std::enable_if<!std::numeric_limits< T >::is_integer, T >::type machineEpsilon()
Calculates the smallest floating point value approximation the given type T can represent,...
Definition: float_math.hpp:62
jau::almost_equal
std::enable_if<!std::numeric_limits< T >::is_integer, bool >::type almost_equal(const T &a, const T &b, int ulp=1, const T &epsilon=std::numeric_limits< T >::epsilon())
Returns true, if both floating point values are equal in the sense that their potential difference is...
Definition: float_math.hpp:102
jau::machine_equal
std::enable_if<!std::numeric_limits< T >::is_integer, bool >::type machine_equal(const T &a, const T &b, int ulp=1, const T &epsilon=std::numeric_limits< T >::epsilon())
Returns true, if both floating point values are equal in the sense that their potential difference is...
Definition: float_math.hpp:83