From 925cc3d2ffade5709e2173d69131d298b6b5c2c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 27 Apr 2012 02:28:32 +0200 Subject: [PATCH] Make Math::pow() templated on type. --- src/Math/Math.h | 19 +++++++++++++------ src/Math/Test/MathTest.cpp | 5 +++-- 2 files changed, 16 insertions(+), 8 deletions(-) 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() {