12 constexpr
bool operator()(
const T &lhs,
const T &rhs)
const {
13 static_assert(std::is_arithmetic_v<T>,
14 "In less_abs<T>(), T must be arithmetic");
15 return std::abs(lhs) < std::abs(rhs);
21 template <
typename T,
typename... Args>
22 T
max(T first, Args... rest) {
23 if constexpr (
sizeof...(rest) == 0) {
26 const auto max_rest =
max(rest...);
27 if (first >= max_rest)
33 template <
typename T,
typename... Args>
34 T
min(T first, Args... rest) {
35 if constexpr (
sizeof...(rest) == 0) {
38 const auto min_rest =
min(rest...);
39 if (first <= min_rest)
47 template <
typename T,
typename... Args>
49 static_assert(std::is_arithmetic_v<T>,
50 "In max_abs<T>(), T must be arithmetic");
51 if constexpr (
sizeof...(rest) == 0) {
54 const auto max_rest =
max_abs(rest...);
55 if (std::abs(first) >= std::abs(max_rest))
62 template <
typename T,
typename... Args>
64 static_assert(std::is_arithmetic_v<T>,
65 "In min_abs<T>(), T must be arithmetic");
66 if constexpr (
sizeof...(rest) == 0) {
69 const auto min_rest =
min_abs(rest...);
70 if (std::abs(first) <= std::abs(min_rest))
77 template <
typename T,
typename... Args>
79 static_assert(std::is_arithmetic_v<T>,
80 "In max_difference<T>(), T must be arithmetic");
81 return max(first, rest...) -
min(first, rest...);
87 template <
int n,
typename T>
88 constexpr
auto pow(T x) {
91 if constexpr (n < 0) {
92 return double(1.0) /
pow<-n>(x);
93 }
else if constexpr (n == 0) {
95 return static_cast<T
>(1);
96 }
else if constexpr (n == 1) {
99 return x *
pow<n - 1>(x);
104 template <
typename T>
105 constexpr T
pow(T x,
int n) {
106 static_assert(std::is_floating_point_v<T>,
107 "In pow(T x, int n), T must be foating point");
109 return static_cast<T
>(1) / pow<T>(x, -n);
113 for (
int i = 0; i < n; ++i) {
121 template <
typename T>
123 static_assert(std::is_arithmetic_v<T>,
124 "In sign(T value), T must be arithmetic");
125 return (T(0) < value) - (value < T(0));
130 template <
typename T>
132 static_assert(std::is_arithmetic_v<T>,
133 "In clip(T value, T max_abs), T must be arithmetic");
142 template <
typename T>
144 static_assert(std::is_arithmetic_v<T>,
145 "In clip(T value, T max_abs), T must be arithmetic");
147 return static_cast<T
>(0);
namespace qip::overloads provides operator overloads for std::vector
Definition: Vector.hpp:450
qip library: A collection of useful functions
Definition: Array.hpp:8
T max_difference(T first, Args... rest)
Returns max{args..} - min{args...}, for any number of args (variadic)
Definition: Maths.hpp:78
T max(T first, Args... rest)
Returns maximum of any number of parameters (variadic function)
Definition: Maths.hpp:22
constexpr int sign(T value)
Returns sign of value. Note: sign(0)==0.
Definition: Maths.hpp:122
T max_abs(T first, Args... rest)
Returns value with maximum absolute value of any number of parameters (variadic function)
Definition: Maths.hpp:48
T min(T first, Args... rest)
Returns minimum of any number of parameters (variadic function)
Definition: Maths.hpp:34
constexpr T chop(T value, T min_abs)
Sets values |v|<min to zero; if |v|>=min, returns v.
Definition: Maths.hpp:143
T min_abs(T first, Args... rest)
Returns value with minimum absolute value of any number of parameters (variadic function)
Definition: Maths.hpp:63
constexpr auto pow(T x)
x^n for integer n (n compile-time template parameter), x any arithmetic type (T). Returns double for ...
Definition: Maths.hpp:88
constexpr T clip(T value, T max_abs)
Clips value to between -max <= value <= max clip(x,max) : |x| > max, ret max; if |x|<-max,...
Definition: Maths.hpp:131
Function object for performing comparisons of absolute values (uses std::abs). Works similarly to std...
Definition: Maths.hpp:10