From 5516d7333591dd1bea9460cc6d50fc26379f65aa Mon Sep 17 00:00:00 2001 From: Daniel Arndt Date: Fri, 24 Aug 2018 19:16:25 +0200 Subject: [PATCH] Use Assert instead, lower the standard required and add comments --- include/deal.II/base/utilities.h | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index e469558f59..03ee0fbfbd 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -353,9 +353,26 @@ namespace Utilities constexpr unsigned int pow(const unsigned int base, const int iexp) { -#ifdef DEAL_II_WITH_CXX17 - AssertThrow(iexp >= 0, ExcMessage("The exponent must not be negative!")); +#ifdef DEAL_II_WITH_CXX14 + Assert(iexp >= 0, ExcMessage("The exponent must not be negative!")); #endif + // The "exponentiation by squaring" algorithm used below has to be + // compressed to one statement due to C++11's restrictions on constexpr + // functions. A more descriptive version would be: + // + // + // if (iexp <= 0) + // return 1; + // + // // if the current exponent is not divisible by two, + // // we need to account for that. + // const unsigned int prefactor = (iexp % 2 == 1) ? base : 1; + // + // // a^b = (a*a)^(b/2) for b evenb + // // a^b = a*(a*a)^((b-1)/2 for b odd + // return prefactor * dealii::Utilities::pow(base*base, iexp/2); + // + return iexp <= 0 ? 1 : (((iexp % 2 == 1) ? base : 1) * dealii::Utilities::pow(base * base, iexp / 2)); -- 2.39.5