From: Martin Kronbichler Date: Mon, 30 Mar 2020 06:21:26 +0000 (+0200) Subject: Avoid unnecessary integer overflow of Utilities::pow() X-Git-Tag: v9.2.0-rc1~327^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=165397e2c5d20853385c9540204a35df57505112;p=dealii.git Avoid unnecessary integer overflow of Utilities::pow() --- diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index a1db035464..04218bd02b 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -508,6 +508,10 @@ namespace Utilities // if (iexp <= 0) // return 1; // + // // avoid overflow of one additional recursion with pow(base * base, 0) + // if (iexp == 1) + // return base; + // // // if the current exponent is not divisible by two, // // we need to account for that. // const unsigned int prefactor = (iexp % 2 == 1) ? base : 1; @@ -519,9 +523,11 @@ namespace Utilities static_assert(std::is_integral::value, "Only integral types supported"); - return iexp <= 0 ? 1 : - (((iexp % 2 == 1) ? base : 1) * - dealii::Utilities::pow(base * base, iexp / 2)); + return iexp <= 0 ? + 1 : + (iexp == 1 ? base : + (((iexp % 2 == 1) ? base : 1) * + dealii::Utilities::pow(base * base, iexp / 2))); } /**