From: Daniel Arndt Date: Fri, 24 Aug 2018 09:29:03 +0000 (+0200) Subject: Assert positive exponents in Utilities::pow X-Git-Tag: v9.1.0-rc1~772^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=31d6094e306bc58a7ec1e8017c40c45c82645bc4;p=dealii.git Assert positive exponents in Utilities::pow --- diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index bcb1e8a33f..e469558f59 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -347,12 +347,18 @@ namespace Utilities /** * A replacement for std::pow that allows compile-time - * calculations for constant expression arguments. + * calculations for constant expression arguments. The exponent @p iexp + * must not be negative. */ constexpr unsigned int - pow(const unsigned int base, const unsigned int iexp) + pow(const unsigned int base, const int iexp) { - return iexp == 0 ? 1 : base * dealii::Utilities::pow(base, iexp - 1); +#ifdef DEAL_II_WITH_CXX17 + AssertThrow(iexp >= 0, ExcMessage("The exponent must not be negative!")); +#endif + return iexp <= 0 ? 1 : + (((iexp % 2 == 1) ? base : 1) * + dealii::Utilities::pow(base * base, iexp / 2)); } /**