25#ifndef JAU_BASE_MATH_HPP_
26#define JAU_BASE_MATH_HPP_
62 template<jau::req::arithmetic T>
63 bool in_range(
const T& a,
const T& b,
const T& range) {
64 return std::abs(a-b) <= range;
75 requires std::floating_point<T>
101 template <jau::req::
signed_arithmetic T>
102 constexpr int sign(
const T x)
noexcept
104 return (
int) ( (T(0) < x) - (x < T(0)) );
107 template <jau::req::
unsigned_arithmetic T>
108 constexpr int sign(
const T x)
noexcept
110 return (
int) ( T(0) < x );
139 template <jau::req::
signed_arithmetic T>
142 return std::numeric_limits<T>::min() == x ? std::numeric_limits<T>::max() : -x;
145 template <jau::req::
unsigned_arithmetic T>
164 template <jau::req::
signed_arithmetic T>
165 constexpr T
abs(
const T x)
noexcept
170 template <jau::req::
unsigned_arithmetic T>
171 constexpr T
abs(
const T x)
noexcept
185 template<jau::req::
signed_arithmetic T>
187 using U = std::make_unsigned_t<T>;
189 if (std::numeric_limits<T>::min() == x) {
190 return U(std::numeric_limits<T>::max()) + U(1);
199 template <jau::req::
unsigned_arithmetic T>
212 template <jau::req::arithmetic T>
213 constexpr T
min(
const T x,
const T y)
noexcept
215 return x < y ? x : y;
225 template <jau::req::arithmetic T>
226 constexpr T
max(
const T x,
const T y)
noexcept
228 return x > y ? x : y;
241 template <jau::req::arithmetic T>
242 constexpr T
clamp(
const T x,
const T min_val,
const T max_val)
noexcept
Concept of type-trait std::is_signed and std::is_integral.
Concept of type-trait std::is_unsigned and std::is_integral.
constexpr T invert_sign(const T x) noexcept
Safely inverts the sign of an arithmetic number w/ branching in O(1)
constexpr T clamp(const T x, const T min_val, const T max_val) noexcept
Returns constrained integral value to lie between given min- and maximum value (w/ branching) in O(1)...
constexpr int sign(const T x) noexcept
Returns the value of the sign function (w/o branching ?) in O(1).
constexpr bool is_positive(const T a) noexcept
Returns true of the given integral is positive, i.e.
constexpr std::make_unsigned_t< T > unsigned_value(const T x) noexcept
Returns the unsigned typed absolute value of an arithmetic number (w/ branching) in O(1)
constexpr T min(const T x, const T y) noexcept
Returns the minimum of two integrals (w/ branching) in O(1)
bool in_range(const T &a, const T &b, const T &range)
base_math: arithmetic types, i.e.
constexpr T max(const T x, const T y) noexcept
Returns the maximum of two integrals (w/ branching) in O(1)
constexpr T abs(const T x) noexcept
Returns the absolute value of an arithmetic number (w/ branching) in O(1)
__pack(...): Produces MSVC, clang and gcc compatible lead-in and -out macros.