commonlib
mathfuncs.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include "mathematics.h"
14 #include <cmath>
15 #include <limits>
16 #include <numeric>
17 #include <cassert>
18 
19 BEGIN_NAMESPACE_MATHEMATICS
20 
24 class MathFuncs
25 {
26 public:
34  template<typename T>
35  static bool inRange(T input, T minimum, T maximum);
36 
44  template<typename T>
45  static T ensureRange(T input, T minimum, T maximum);
46 
54  template<typename T>
55  static bool areSame(T first, T second, T epsilon = (T) std::numeric_limits<T>::epsilon());
56 
63  template<typename T>
64  static bool isZero(T input, T epsilon = (T) std::numeric_limits<T>::epsilon());
65 
72  template<typename T>
73  static T gcd(T first, T second);
74 
81  template<typename T>
82  static T lcm(T first, T second);
83 };
84 
85 template<typename T>
86 bool MathFuncs::inRange(T input, T minimum, T maximum)
87 {
88  return (minimum <= input) && (input <= maximum);
89 }
90 
91 template<typename T>
92 T MathFuncs::ensureRange(T input, T minimum, T maximum)
93 {
94  assert(minimum <= maximum);
95  if (input < minimum)
96  return minimum;
97  if (input > maximum)
98  return maximum;
99  return input;
100 }
101 
102 template <typename T>
103 bool MathFuncs::areSame(T first, T second, T epsilon)
104 {
105  static_assert(std::is_arithmetic_v<T>, "areSame type not supported.");
106 
107  if constexpr (std::is_integral<T>::value)
108  {
109  if constexpr (!std::is_unsigned<T>::value)
110  {
111  if (epsilon < 0)
112  epsilon = -epsilon; // compiler doesn't allow this for unsigned types
113  }
114 
115  T max = first;
116  T min = second;
117  if (first < second)
118  {
119  max = second;
120  min = first;
121  }
122  return max - min <= epsilon;
123  }
124  return std::fabs(first - second) <= std::fabs(epsilon);
125 }
126 
127 template<typename T>
128 bool MathFuncs::isZero(T input, T epsilon)
129 {
130  return areSame(input, T(0), epsilon);
131 }
132 
133 template<typename T>
134 T MathFuncs::gcd(T first, T second)
135 {
136  return std::gcd(first, second);
137 }
138 
139 template<typename T>
140 T MathFuncs::lcm(T first, T second)
141 {
142  return std::lcm(first, second);
143 }
144 
145 END_NAMESPACE_MATHEMATICS
static T lcm(T first, T second)
Function returns lowest common multiple for for two values.
Definition: mathfuncs.h:140
static T gcd(T first, T second)
Function returns greatest common divisor for two values.
Definition: mathfuncs.h:134
static bool inRange(T input, T minimum, T maximum)
Indicates whether a value falls within a specified range.
Definition: mathfuncs.h:86
static bool isZero(T input, T epsilon=(T) std::numeric_limits< T >::epsilon())
Function checks if value is zero.
Definition: mathfuncs.h:128
The class implements helper functions for mathematical operations.
Definition: mathfuncs.h:24
static T ensureRange(T input, T minimum, T maximum)
Function returns a value that is guaranteed to be within a specified range.
Definition: mathfuncs.h:92
static bool areSame(T first, T second, T epsilon=(T) std::numeric_limits< T >::epsilon())
Function checks if two values are the same within data types limitation.
Definition: mathfuncs.h:103