diff --git a/bezier3.cpp b/bezier3.cpp index 0721b924..d9262b82 100644 --- a/bezier3.cpp +++ b/bezier3.cpp @@ -30,10 +30,10 @@ namespace util { CHECK_GE (t, 0); CHECK_LE (t, 1); - auto v0 = pow (1 - t, 3) * m_points[0]; + auto v0 = pow (1 - t, 3u) * m_points[0]; auto v1 = 3 * pow2 (1 - t) * t * m_points[1]; auto v2 = 3 * pow2 (1 - t) * t * m_points[2]; - auto v3 = pow (t, 3) * m_points[3]; + auto v3 = pow (t, 3u) * m_points[3]; return { v0.x + v1.x + v2.x + v3.x, diff --git a/cmdopt.cpp b/cmdopt.cpp index 875d9d27..402d8baf 100644 --- a/cmdopt.cpp +++ b/cmdopt.cpp @@ -192,27 +192,27 @@ suffix_to_multiplier (char c) switch (c) { case 'e': case 'E': - return util::pow (1024UL, 6); + return util::pow (1024UL, 6u); case 'p': case 'P': - return util::pow (1024UL, 5); + return util::pow (1024UL, 5u); case 't': case 'T': - return util::pow (1024UL, 4); + return util::pow (1024UL, 4u); case 'g': case 'G': - return util::pow (1024UL, 3); + return util::pow (1024UL, 3u); case 'm': case 'M': - return util::pow (1024UL, 2); + return util::pow (1024UL, 2u); case 'k': case 'K': - return util::pow (1024UL, 1); + return util::pow (1024UL, 1u); default: const char str[2] = { c, '\0' }; diff --git a/maths.hpp b/maths.hpp index 24ad136b..8088a3ff 100644 --- a/maths.hpp +++ b/maths.hpp @@ -221,11 +221,19 @@ namespace util { /////////////////////////////////////////////////////////////////////////// - template - constexpr T - pow [[gnu::const]] (T x, unsigned y) + template < + typename BaseT, + typename ExponentT, + typename = std::enable_if_t< + std::is_unsigned_v, + void + > + > + constexpr BaseT + pow [[gnu::const]] (BaseT base, ExponentT exponent) { - return y == 0 ? T{1} : x * pow (x, y - 1); + assert (exponent >= 0); + return exponent == 0 ? BaseT{1} : base * pow (base, exponent - 1); } diff --git a/test/roots/bisection.cpp b/test/roots/bisection.cpp index f0e763b7..81402655 100644 --- a/test/roots/bisection.cpp +++ b/test/roots/bisection.cpp @@ -8,9 +8,9 @@ using util::pow; constexpr float order2 (float x) { return x * x + 3 * x - 7.f; } -constexpr float order4 (float x) { return 10 * pow (x, 4u) - -270 * pow (x, 2u) - -140 * pow (x, 1u) +constexpr float order4 (float x) { return 10 * util::pow (x, 4u) + -270 * util::pow (x, 2u) + -140 * util::pow (x, 1u) +1200; } struct {