]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Use Assert instead, lower the standard required and add comments 7113/head
authorDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Fri, 24 Aug 2018 17:16:25 +0000 (19:16 +0200)
committerDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Fri, 24 Aug 2018 17:16:25 +0000 (19:16 +0200)
include/deal.II/base/utilities.h

index e469558f5987a7b86c49afa1eacf86a271b49f91..03ee0fbfbd9a84b3618891042b5c5dcfab852440 100644 (file)
@@ -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:
+    //
+    // <code>
+    // 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);
+    // </code>
+
     return iexp <= 0 ? 1 :
                        (((iexp % 2 == 1) ? base : 1) *
                         dealii::Utilities::pow(base * base, iexp / 2));

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.