From: Daniel Arndt Date: Fri, 24 Aug 2018 17:16:25 +0000 (+0200) Subject: Use Assert instead, lower the standard required and add comments X-Git-Tag: v9.1.0-rc1~772^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5516d7333591dd1bea9460cc6d50fc26379f65aa;p=dealii.git Use Assert instead, lower the standard required and add comments --- 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));