Created
December 1, 2021 15:55
-
-
Save neacsum/7fec051280b5770b4dbacb8a8a11320e to your computer and use it in GitHub Desktop.
Integer exponentiation template function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
/* | |
Integer exponentiation function. | |
In most cases, it is faster than standard pow function | |
*/ | |
template <typename T> | |
T ipow (T base, int exp) | |
{ | |
T result = 1; | |
while (exp) | |
{ | |
if (exp & 1) | |
result *= base; | |
exp >>= 1; | |
base *= base; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment