Skip to content

Commit

Permalink
fix issues with min max and clamp
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinzii committed Aug 20, 2023
1 parent ee7ff6d commit 63c8cbb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 76 deletions.
54 changes: 12 additions & 42 deletions include/mim/cmath.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,73 +13,43 @@
#include <limits>
#include <type_traits>
#include <cmath>
#include <algorithm>

#include "mim/mimConstants.hpp"

namespace mim::math
{



template <typename T>
[[nodiscard]] constexpr bool isnan(const T& val) noexcept
{
return val != val;
}

template <typename T>
[[nodiscard]] constexpr bool isnan(T& val) noexcept
{
return val != val;
}

template <typename T>
[[nodiscard]] constexpr T abs(const T& val) noexcept
{
return val == 0 ? 0 : (val < 0 ? -val : val);
}

template <typename T, typename... Args>
[[nodiscard]] constexpr auto min(const T&& a, const T&& b, const Args&&... args) noexcept
{
if constexpr (sizeof...(args) == 0) {
return (a < b) ? std::forward<T>(a) : std::forward<T>(b);
} else {
return ::mim::math::min(::mim::math::min(std::forward<T>(a), std::forward<T>(b)), std::forward<Args>(args)...);
}
}

template <typename T, typename... Args>
[[nodiscard]] constexpr auto min(T&& a, T&& b, Args&&... args) noexcept
{
if constexpr (sizeof...(args) == 0) {
return (a < b) ? std::forward<T>(a) : std::forward<T>(b);
} else {
return ::mim::math::min(::mim::math::min(std::forward<T>(a), std::forward<T>(b)), std::forward<Args>(args)...);
}
}

template <typename T, typename... Args>
[[nodiscard]] constexpr auto max(const T&& a, const T&& b, const Args&&... args) noexcept
template <typename T>
[[nodiscard]] constexpr T abs(T& val) noexcept
{
if constexpr (sizeof...(args) == 0) {
return (a < b) ? std::forward<T>(b) : std::forward<T>(a);
} else {
return ::mim::math::max(::mim::math::max(std::forward<T>(a), std::forward<T>(b)), std::forward<Args>(args)...);
}

return val == 0 ? 0 : (val < 0 ? -val : val);
}

template <typename T, typename... Args>
[[nodiscard]] constexpr auto max(T&& a, T&& b, Args&&... args) noexcept
{
if constexpr (sizeof...(args) == 0) {
return (a < b) ? std::forward<T>(b) : std::forward<T>(a);
} else {
return ::mim::math::max(::mim::math::max(std::forward<T>(a), std::forward<T>(b)), std::forward<Args>(args)...);
}
}

namespace detail
{
template <typename T>
[[nodiscard]] constexpr bool isclose(T a, T b, T epsilon = std::numeric_limits<T>::epsilon()) noexcept
{
return ::mim::math::abs(a - b) <= epsilon * ::mim::math::max(::mim::math::abs(a), ::mim::math::abs(b));
return ::mim::math::abs(a - b) <= epsilon * std::max(::mim::math::abs(a), ::mim::math::abs(b));
}

template <typename T>
Expand All @@ -92,7 +62,7 @@ namespace mim::math
template <typename T>
[[nodiscard]] constexpr auto clamp(const T& val, const T& lo, const T& hi) noexcept
{
return ::mim::math::min(::mim::math::max(val, lo), hi);
return std::min(std::max(val, lo), hi);
}

template <typename T>
Expand Down
35 changes: 1 addition & 34 deletions test/common/cmath_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#include <gtest/gtest.h>
#include <mim/cmath.hpp>
#include <limits>
#include <cmath>
#include <cfloat>



TEST(cmathDefaultTests, isnan)
Expand All @@ -29,38 +28,6 @@ TEST(cmathDefaultTests, abs)
EXPECT_EQ(mim::math::abs(-1.0l), 1.0l);
}

TEST(cmathDefaultTests, min)
{
EXPECT_EQ(mim::math::min(0, 1), 0);
EXPECT_EQ(mim::math::min(1, 0), 0);
EXPECT_EQ(mim::math::min(0.0f, 1.0f), 0.0f);
EXPECT_EQ(mim::math::min(1.0f, 0.0f), 0.0f);
EXPECT_EQ(mim::math::min(0.0, 1.0), 0.0);
EXPECT_EQ(mim::math::min(1.0, 0.0), 0.0);
EXPECT_EQ(mim::math::min(0.0l, 1.0l), 0.0l);
EXPECT_EQ(mim::math::min(1.0l, 0.0l), 0.0l);

EXPECT_EQ(mim::math::min(0, 1, 2), 0);
EXPECT_EQ(mim::math::min(1, 0, 2), 0);
EXPECT_EQ(mim::math::min(2, 1, 0, 5, 8, 19), 0);

}

TEST(cmathDefaultTests, max)
{
EXPECT_EQ(mim::math::max(0, 1), 1);
EXPECT_EQ(mim::math::max(1, 0), 1);
EXPECT_EQ(mim::math::max(0.0f, 1.0f), 1.0f);
EXPECT_EQ(mim::math::max(1.0f, 0.0f), 1.0f);
EXPECT_EQ(mim::math::max(0.0, 1.0), 1.0);
EXPECT_EQ(mim::math::max(1.0, 0.0), 1.0);
EXPECT_EQ(mim::math::max(0.0l, 1.0l), 1.0l);
EXPECT_EQ(mim::math::max(1.0l, 0.0l), 1.0l);

EXPECT_EQ(mim::math::max(0, 1, 2), 2);
EXPECT_EQ(mim::math::max(1, 0, 2), 2);
EXPECT_EQ(mim::math::max(2, 1, 0, 5, 8, 19), 19);
}

TEST(cmathDefaultTests, clamp)
{
Expand Down

0 comments on commit 63c8cbb

Please sign in to comment.