smatskevich

Power

Mar 13th, 2021 (edited)
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.39 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. double pow(double a, unsigned int n) {
  4.   double result = 1.;
  5.   double a_in_power_of_2 = a;
  6.  
  7.   while (n) {
  8.     if ((n & 1u) == 1u)
  9.       result *= a_in_power_of_2;
  10.     a_in_power_of_2 *= a_in_power_of_2;
  11.     n >>= 1u;
  12.   }
  13.   return result;
  14. }
  15.  
  16. int main() {
  17.   double a = .0;
  18.   unsigned int n = 0;
  19.   std::cin >> a >> n;
  20.   std::cout << pow(a, n);
  21.   return 0;
  22. }
Add Comment
Please, Sign In to add comment