diff --git a/src/Math/Math.h b/src/Math/Math.h index 827ba6c81..f651a812e 100644 --- a/src/Math/Math.h +++ b/src/Math/Math.h @@ -52,6 +52,17 @@ template<> struct Constants { static constexpr float Sqrt2 = 1.41421356237f; static constexpr float Sqrt3 = 1.73205080757f; }; + +namespace Implementation { + template struct Pow { + template inline constexpr T operator()(T base) const { + return base*Pow()(base); + } + }; + template<> struct Pow<0> { + template inline constexpr T operator()(T base) const { return 1; } + }; +} #endif /** @@ -59,14 +70,10 @@ template<> struct Constants { * * Returns integral power of base to the exponent. */ -template inline constexpr size_t pow(size_t base) { - return base*pow(base); +template inline constexpr T pow(T base) { + return Implementation::Pow()(base); } -#ifndef DOXYGEN_GENERATING_OUTPUT -template<> inline constexpr size_t pow<0>(size_t base) { return 1; } -#endif - /** * @brief Integral logarithm * diff --git a/src/Math/Test/MathTest.cpp b/src/Math/Test/MathTest.cpp index 63080e8a3..eb823548c 100644 --- a/src/Math/Test/MathTest.cpp +++ b/src/Math/Test/MathTest.cpp @@ -30,8 +30,9 @@ void MathTest::degrad() { } void MathTest::pow() { - QCOMPARE(Math::pow<10>(2), 1024ul); - QCOMPARE(Math::pow<0>(3), 1ul); + QCOMPARE(Math::pow<10>(2ul), 1024ul); + QCOMPARE(Math::pow<0>(3ul), 1ul); + QCOMPARE(Math::pow<2>(2.0f), 4.0f); } void MathTest::log() {