variadic min/max

This commit is contained in:
BlackMATov
2020-12-06 05:38:00 +07:00
parent 422d251df1
commit b0659e9239
3 changed files with 32 additions and 2 deletions

View File

@@ -617,6 +617,9 @@ T acosh(T x) noexcept;
template < floating_point T > template < floating_point T >
T atanh(T x) noexcept; T atanh(T x) noexcept;
template < floating_point T >
std::pair<T, T> sincos(T x) noexcept;
template < floating_point T > template < floating_point T >
void sincos(T x, T* s, T* c) noexcept; void sincos(T x, T* s, T* c) noexcept;
@@ -759,9 +762,15 @@ T modf(T x, T* y) noexcept;
template < arithmetic T > template < arithmetic T >
constexpr T min(T x, T y) noexcept; 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 > template < arithmetic T >
constexpr T max(T x, T y) noexcept; 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 > template < arithmetic T >
constexpr T clamp(T x, T min_x, T max_x) noexcept; constexpr T clamp(T x, T min_x, T max_x) noexcept;

View File

@@ -86,12 +86,28 @@ namespace vmath_hpp
return x < y ? x : y; 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 > template < typename T >
[[nodiscard]] std::enable_if_t<std::is_arithmetic_v<T>, T> [[nodiscard]] std::enable_if_t<std::is_arithmetic_v<T>, T>
constexpr max(T x, T y) noexcept { constexpr max(T x, T y) noexcept {
return x < y ? y : x; 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 > template < typename T >
[[nodiscard]] std::enable_if_t<std::is_arithmetic_v<T>, T> [[nodiscard]] std::enable_if_t<std::is_arithmetic_v<T>, T>
constexpr clamp(T x, T min_x, T max_x) noexcept { constexpr clamp(T x, T min_x, T max_x) noexcept {

View File

@@ -85,8 +85,13 @@ TEST_CASE("vmath/fun") {
REQUIRE(out_i == approx(1.f)); REQUIRE(out_i == approx(1.f));
} }
STATIC_REQUIRE(min(1.f, 2.f) == approx(1.f)); STATIC_REQUIRE(min(0.f, 1.f) == approx(0.f));
STATIC_REQUIRE(max(1.f, 2.f) == approx(2.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(1.0f, 2.f, 3.f) == approx(2.0f));
STATIC_REQUIRE(clamp(2.5f, 2.f, 3.f) == approx(2.5f)); STATIC_REQUIRE(clamp(2.5f, 2.f, 3.f) == approx(2.5f));