]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Assert positive exponents in Utilities::pow
authorDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Fri, 24 Aug 2018 09:29:03 +0000 (11:29 +0200)
committerDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Fri, 24 Aug 2018 09:29:03 +0000 (11:29 +0200)
include/deal.II/base/utilities.h

index bcb1e8a33fb7f411cb05a63728ffb8ca35ae5fd4..e469558f5987a7b86c49afa1eacf86a271b49f91 100644 (file)
@@ -347,12 +347,18 @@ namespace Utilities
 
   /**
    * A replacement for <code>std::pow</code> 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));
   }
 
   /**

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.