diff --git a/math/fast_power.cpp b/math/fast_power.cpp index 0ffbcd40d..f69a90e4c 100644 --- a/math/fast_power.cpp +++ b/math/fast_power.cpp @@ -21,16 +21,17 @@ /** * algorithm implementation for \f$a^b\f$ */ -double fast_power_recursive(int64_t a, int64_t b) { +template +double fast_power_recursive(T a, T b) { // negative power. a^b = 1 / (a^-b) if (b < 0) return 1.0 / fast_power_recursive(a, -b); if (b == 0) return 1; - int64_t bottom = fast_power_recursive(a, b >> 1); + T bottom = fast_power_recursive(a, b >> 1); // Since it is integer division b/2 = (b-1)/2 where b is odd. // Therefore, case2 is easily solved by integer division. - int64_t result; + double result; if ((b & 1) == 0) // case1 result = bottom * bottom; else // case2 @@ -42,7 +43,8 @@ double fast_power_recursive(int64_t a, int64_t b) { Same algorithm with little different formula. It still calculates in O(logN) */ -double fast_power_linear(int64_t a, int64_t b) { +template +double fast_power_linear(T a, T b) { // negative power. a^b = 1 / (a^-b) if (b < 0) return 1.0 / fast_power_linear(a, -b);