From: Matthias Maier Date: Thu, 5 Sep 2019 23:08:55 +0000 (-0500) Subject: Utilities::fixed_power - use exponentiation by squaring X-Git-Tag: v9.2.0-rc1~1136^2~3 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a7d76ae20aed647673dce37334b04917fbe41012;p=dealii.git Utilities::fixed_power - use exponentiation by squaring --- diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index 00442d9d9d..4c9ae69d84 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -1030,27 +1030,15 @@ namespace Utilities { template inline T - fixed_power(const T n) + fixed_power(const T x) { - Assert(N >= 0, ExcNotImplemented()); - switch (N) - { - case 0: - return dealii::internal::NumberType::value(1); - case 1: - return n; - case 2: - return n * n; - case 3: - return n * n * n; - case 4: - return n * n * n * n; - default: - T result = n; - for (int d = 1; d < N; ++d) - result *= n; - return result; - } + if (N == 0) + return T(1.); + else if (N < 0) + return T(1.) / fixed_power<-N>(x); + else + // Use exponentiation by squaring: + return ((N % 2 == 1) ? x * fixed_power(x * x) : fixed_power(x * x)); }