13#if __cplusplus <= 201703L
16inline constexpr T
pi_v =
static_cast<T
>(3.141592653589793238462643383279502884L);
21inline constexpr T
pi_v = ::std::numbers::pi_v<T>;
33 static_assert(std::is_arithmetic_v<T>,
"Type T must be an arithmetic type.");
34 if constexpr (std::is_integral_v<T>)
36 return (value + rnd / T(2)) / rnd * rnd;
38 if constexpr (std::is_floating_point_v<T>)
41 return std::floor(value / rnd + T(0.5)) * rnd;
49template<
typename T,
typename S>
54 S temp = max && value ? (std::is_floating_point_v<T> ? len * (value / max) : len * value / max) : 0;
61 return temp < len ? len : temp > S(0) ? S(0) : temp;
63 return temp > len ? len : temp < S(0) ? S(0) : temp;
77T
clip(
const T value,
const T lower_lim,
const T upper_lim)
79 return value < lower_lim ? lower_lim : value > upper_lim ? upper_lim : value;
88 static std::default_random_engine generator{};
89 std::uniform_int_distribution<T> distribution(start, stop);
90 return distribution(generator);
104 static_assert(std::is_arithmetic_v<T>,
"Type T must be an arithmetic type.");
106 if constexpr (std::is_floating_point_v<T>)
109 T r = std::fmod(k, n);
112 return (n > 0 && r < 0) || (n < 0 && r > 0) ? r + n : r;
119 return (n > 0 && r < 0) || (n < 0 && r > 0) ? r + n : r;
132 static_assert(std::is_arithmetic_v<T>,
"Type T must be an arithmetic type.");
133 if constexpr (std::is_same<T, long double>())
137 if constexpr (std::is_same<T, double>())
141 if constexpr (std::is_same<T, float>())
145 if constexpr (std::is_integral_v<T>)
147 if constexpr (std::numeric_limits<T>::is_signed)
149 if constexpr (std::is_same<T, long long int>())
151 return std::llabs(v);
153 if constexpr (std::is_same<T, long int>())
157 if constexpr (std::is_same<T, int>())
162 return static_cast<T
>(std::abs(v));
180 static_assert(std::is_integral_v<T>,
"Type T must be an integer type.");
210 static_assert(std::is_floating_point_v<T>,
"Type T must be a floating point type.");
211 return degrees * (numbers::pi_v<T> / 180);
221 static_assert(std::is_floating_point_v<T>,
"Type T must be a floating point type.");
222 return radians / numbers::pi_v<T> * 180;
234bool isEqual(T a, T b, T epsilon = std::numeric_limits<T>::epsilon())
237 static_assert(std::is_floating_point_v<T>,
"Type T must be a floating point type.");
238 return std::abs(a - b) <= epsilon;
245bool isZero(T value, T epsilon = std::numeric_limits<T>::epsilon())
247 static_assert(std::is_arithmetic_v<T>,
"Type T must be an arithmetic type.");
248 if constexpr (std::is_integral_v<T>)
250 return value == std::numeric_limits<T>::denorm_min();
252 if constexpr (std::is_floating_point_v<T>)
254 return std::fabs(value) < epsilon;
265 static_assert(std::is_floating_point_v<T>,
"Type T must be an floating point type.");
266 return !std::isinf(value) && !std::isnan(value);
272template<
typename T,
typename... Args>
279 auto compare = [&largest, &idx, &idx_cur](T value) {
287 (compare(first), ..., compare(args));
294template<
typename T,
typename... Args>
301 auto compare = [&largest, &idx, &idx_cur](T value) {
309 (compare(first), ..., compare(args));
320template<
typename T,
size_t N>
325 for (
size_t i = 1; i < arr.size(); ++i)
343template<
typename T,
size_t N>
348 for (
size_t i = 1; i < arr.size(); ++i)
388 static_assert(std::is_floating_point_v<T>,
"Type T must be a floating point type.");
392 auto exponent = std::floor(std::log10(std::abs(value)));
393 auto mantissa = value / std::pow(10, exponent);
395 if (std::abs(mantissa) < 0.1)
400 else if (abs(mantissa) >= 1.0)
405 return {mantissa, exponent};
#define _MISC_FUNC
Definition misc/global.h:39
constexpr T pi_v
Definition misc/gen/math.h:16
Definition Application.h:10
size_t minArgumentIndex(T first, Args... args)
Finds the index of the smallest element passed as an argument.
Definition misc/gen/math.h:295
T ipow(T base, int exponent)
Fast integer power-of function.
Definition misc/gen/math.h:177
size_t minArrayIndex(const std::array< T, N > &arr)
Finds the largest element index of the passed array.
Definition misc/gen/math.h:344
int magnitude(T value)
Gets the order of magnitude of the passed floating point value. Examples: magnitude(0....
Definition misc/gen/math.h:416
T modulo(T k, T n)
Modulo template function.
Definition misc/gen/math.h:101
constexpr T toRadians(T degrees)
Converts degrees to radians.
Definition misc/gen/math.h:207
T clip(const T value, const T lower_lim, const T upper_lim)
Template function clipping the given value between an upper and lower limit.
Definition misc/gen/math.h:77
size_t maxArgumentIndex(T first, Args... args)
Finds the index of the largest element passed as an argument.
Definition misc/gen/math.h:273
bool isEqual(T a, T b, T epsilon=std::numeric_limits< T >::epsilon())
Compares 2 floating point values of the same type with a given epsilon.
Definition misc/gen/math.h:234
constexpr T toDegrees(T radians)
Converts radians to degrees.
Definition misc/gen/math.h:218
size_t maxArrayIndex(const std::array< T, N > &arr)
Finds the largest element index of the passed array.
Definition misc/gen/math.h:321
_MISC_FUNC int precision(double value)
Returns the precision of the passed floating point value. This is the amount of characters after the ...
std::pair< T, int > getMantissaExponent(T value)
Gets the mantissa and exponent part of a floating point value.
Definition misc/gen/math.h:385
_MISC_FUNC int magnitudeObsolete(double value)
Gets the order of magnitude of the passed double value. Examples: magnitude(0.001234) => -2 magnitude...
bool isZero(T value, T epsilon=std::numeric_limits< T >::epsilon())
Check if the passed value is zero or near zero according the given epsilon.
Definition misc/gen/math.h:245
T random(T start, T stop)
Generates random numbers within a specified integer range.
Definition misc/gen/math.h:86
T round(T value, T rnd)
Rounds the passed value to a multiple of the rnd value.
Definition misc/gen/math.h:30
_MISC_FUNC int digits(double value)
Returns the amount of digits which are significant for the value. When the value is 12300....
S calculateOffset(T value, T min, T max, S len, bool clip)
Calculates the offset for a given range and set point.
Definition misc/gen/math.h:50
bool isValidValue(T value)
Check if the passed value is not NaN or infinity.
Definition misc/gen/math.h:263
T toAbs(T v)
Gets the absolute value of a passed scalar value.
Definition misc/gen/math.h:130
_MISC_FUNC int requiredDigits(double round_val, double min_val, double max_val)
Gets the amount of required digits needed when drawing a scale.