mirror of
https://github.com/BlackMATov/vmath.hpp.git
synced 2025-12-14 12:28:58 +07:00
variadic min/max
This commit is contained in:
@@ -617,6 +617,9 @@ T acosh(T x) noexcept;
|
||||
template < floating_point T >
|
||||
T atanh(T x) noexcept;
|
||||
|
||||
template < floating_point T >
|
||||
std::pair<T, T> sincos(T x) noexcept;
|
||||
|
||||
template < floating_point T >
|
||||
void sincos(T x, T* s, T* c) noexcept;
|
||||
|
||||
@@ -759,9 +762,15 @@ T modf(T x, T* y) noexcept;
|
||||
template < arithmetic T >
|
||||
constexpr T min(T x, T y) noexcept;
|
||||
|
||||
template < arithmetic T, arithmetic... Ts >
|
||||
constexpr std::common_type_t<T, Ts...> min(T x, T y, Ts... ts) noexcept;
|
||||
|
||||
template < arithmetic T >
|
||||
constexpr T max(T x, T y) noexcept;
|
||||
|
||||
template < arithmetic T, arithmetic... Ts >
|
||||
constexpr std::common_type_t<T, Ts...> max(T x, T y, Ts... ts) noexcept;
|
||||
|
||||
template < arithmetic T >
|
||||
constexpr T clamp(T x, T min_x, T max_x) noexcept;
|
||||
|
||||
|
||||
@@ -86,12 +86,28 @@ namespace vmath_hpp
|
||||
return x < y ? x : y;
|
||||
}
|
||||
|
||||
template < typename T, typename... Ts >
|
||||
[[nodiscard]] std::enable_if_t<
|
||||
std::is_arithmetic_v<T>,
|
||||
std::common_type_t<T, Ts...>>
|
||||
constexpr min(T x, T y, Ts... ts) noexcept {
|
||||
return min(min(x, y), ts...);
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
[[nodiscard]] std::enable_if_t<std::is_arithmetic_v<T>, T>
|
||||
constexpr max(T x, T y) noexcept {
|
||||
return x < y ? y : x;
|
||||
}
|
||||
|
||||
template < typename T, typename... Ts >
|
||||
[[nodiscard]] std::enable_if_t<
|
||||
std::is_arithmetic_v<T>,
|
||||
std::common_type_t<T, Ts...>>
|
||||
constexpr max(T x, T y, Ts... ts) noexcept {
|
||||
return max(max(x, y), ts...);
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
[[nodiscard]] std::enable_if_t<std::is_arithmetic_v<T>, T>
|
||||
constexpr clamp(T x, T min_x, T max_x) noexcept {
|
||||
|
||||
@@ -85,8 +85,13 @@ TEST_CASE("vmath/fun") {
|
||||
REQUIRE(out_i == approx(1.f));
|
||||
}
|
||||
|
||||
STATIC_REQUIRE(min(1.f, 2.f) == approx(1.f));
|
||||
STATIC_REQUIRE(max(1.f, 2.f) == approx(2.f));
|
||||
STATIC_REQUIRE(min(0.f, 1.f) == approx(0.f));
|
||||
STATIC_REQUIRE(min(3.f, 2.f, 1.f) == approx(1.f));
|
||||
STATIC_REQUIRE(min(4.f, 3.f, 2.f, 1.f) == approx(1.f));
|
||||
|
||||
STATIC_REQUIRE(max(0.f, 1.f) == approx(1.f));
|
||||
STATIC_REQUIRE(max(3.f, 2.f, 1.f) == approx(3.f));
|
||||
STATIC_REQUIRE(max(4.f, 3.f, 2.f, 1.f) == approx(4.f));
|
||||
|
||||
STATIC_REQUIRE(clamp(1.0f, 2.f, 3.f) == approx(2.0f));
|
||||
STATIC_REQUIRE(clamp(2.5f, 2.f, 3.f) == approx(2.5f));
|
||||
|
||||
Reference in New Issue
Block a user