]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Avoid unnecessary integer overflow of Utilities::pow()
authorMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Mon, 30 Mar 2020 06:21:26 +0000 (08:21 +0200)
committerMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Mon, 30 Mar 2020 06:21:26 +0000 (08:21 +0200)
include/deal.II/base/utilities.h

index a1db0354649a8cf8260422d7dcf40815a947d3ad..04218bd02b6e1bebef19d08bd600f5b013d9c1fa 100644 (file)
@@ -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<T>::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)));
   }
 
   /**

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.