25#ifndef JAU_BASE_MATH_HPP_
26#define JAU_BASE_MATH_HPP_
61 std::enable_if_t<std::is_arithmetic_v<T>,
bool> =
true>
62 bool in_range(
const T& a,
const T& b,
const T& range) {
82 std::enable_if_t< std::is_arithmetic_v<T> &&
83 !std::is_unsigned_v<T>,
bool> =
true>
84 constexpr int sign(
const T x)
noexcept
86 return (
int) ( (T(0) < x) - (x < T(0)) );
90 std::enable_if_t< std::is_arithmetic_v<T> &&
91 std::is_unsigned_v<T>,
bool> =
true>
92 constexpr int sign(
const T x)
noexcept
94 return (
int) ( T(0) < x );
123 template <
typename T,
124 std::enable_if_t< std::is_arithmetic_v<T> &&
125 !std::is_unsigned_v<T>,
bool> =
true>
131 template <
typename T,
132 std::enable_if_t< std::is_arithmetic_v<T> &&
133 std::is_unsigned_v<T>,
bool> =
true>
152 template <
typename T,
153 std::enable_if_t< std::is_arithmetic_v<T> &&
154 !std::is_unsigned_v<T>,
bool> =
true>
155 constexpr T
abs(
const T x)
noexcept
157 return jau::sign<T>(x) < 0 ? jau::invert_sign<T>( x ) : x;
160 template <
typename T,
161 std::enable_if_t< std::is_arithmetic_v<T> &&
162 std::is_unsigned_v<T>,
bool> =
true>
163 constexpr T
abs(
const T x)
noexcept
175 template <
typename T,
176 std::enable_if_t< std::is_arithmetic_v<T>,
bool> =
true>
177 constexpr T
min(
const T x,
const T y)
noexcept
179 return x < y ? x : y;
189 template <
typename T,
190 std::enable_if_t< std::is_arithmetic_v<T>,
bool> =
true>
191 constexpr T
max(
const T x,
const T y)
noexcept
193 return x > y ? x : y;
206 template <
typename T,
207 std::enable_if_t< std::is_arithmetic_v<T>,
bool> =
true>
208 constexpr T
clamp(
const T x,
const T min_val,
const T max_val)
noexcept
210 return jau::min<T>(jau::max<T>(x, min_val), max_val);
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 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 int sign(const T x) noexcept
Returns the value of the sign function (w/o branching ?) in O(1).
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.