versatile-mcmc  0.1.7
numeric.hpp
Go to the documentation of this file.
1 
13 #ifndef VMCMC_NUMERIC_H_
14 #define VMCMC_NUMERIC_H_
15 
16 #include <cmath>
17 #include <limits>
18 #include <type_traits>
19 
20 namespace vmcmc {
21 
22 namespace numeric {
23 
24 template <typename T = double>
25 inline T NaN()
26 {
27  return std::numeric_limits<T>::quiet_NaN();
28 }
29 
30 template <typename T = double>
31 inline T inf()
32 {
33  return std::numeric_limits<T>::infinity();
34 }
35 
36 template <typename T,
37 class = typename std::enable_if<std::is_integral<T>::value>::type>
38 inline int numberOfDigits(T number)
39 {
40  if (number == 0)
41  return 1;
42 
43  int nDigits = 0;
44  while (number != 0) {
45  nDigits++;
46  number /= 10;
47  }
48 
49  return nDigits;
50 }
51 
59 template <typename T>
60 inline bool approxEqual(T a, T b, T epsilon)
61 {
62  return std::abs(a - b) <= ( (std::abs(a) < std::abs(b) ? std::abs(b) : std::abs(a)) * epsilon);
63 }
64 
65 template <typename T>
66 inline bool approxLessOrEqual(T a, T b, T epsilon)
67 {
68  return a < b || approximatelyEqual(a, b, epsilon);
69 }
70 
71 template <typename T>
72 inline bool approxGreaterOrEqual(T a, T b, T epsilon)
73 {
74  return a > b || approximatelyEqual(a, b, epsilon);
75 }
76 
84 template <typename T>
85 inline bool essentEqual(T a, T b, T epsilon)
86 {
87  return std::abs(a - b) <= ( (std::abs(a) > std::abs(b) ? std::abs(b) : std::abs(a)) * epsilon);
88 }
89 
90 template <typename T>
91 inline bool essentLessOrEqual(T a, T b, T epsilon)
92 {
93  return a < b || essentiallyEqual(a, b, epsilon);
94 }
95 
96 template <typename T>
97 inline bool essentGreaterOrEqual(T a, T b, T epsilon)
98 {
99  return a > b || essentiallyEqual(a, b, epsilon);
100 }
101 
102 } /* namespace numeric */
103 
104 } /* namespace vmcmc */
105 
106 #endif /* VMCMC_NUMERIC_H_ */
Definition: algorithm.cpp:28
bool essentEqual(T a, T b, T epsilon)
Check whether two float values differ by a given epsilon.
Definition: numeric.hpp:85
bool approxEqual(T a, T b, T epsilon)
Check whether two float values differ by a given epsilon.
Definition: numeric.hpp:60