fork download
  1. #include <iostream>
  2.  
  3. double power(double x, unsigned pow, double _acc = 1)
  4. {
  5. if (pow == 0) return _acc;
  6. if (pow%2)
  7. return power(x, pow - 1, _acc * x);
  8. else
  9. return power(x * x, pow / 2, _acc);
  10. }
  11.  
  12. double power(double x, int y)
  13. {
  14. unsigned pow = static_cast<unsigned>(y >= 0 ? y : -(y+1) + 1UL);
  15. double x_pow = power(x, pow);
  16. return (y >= 0 ? x_pow : 1. / x_pow);
  17. }
  18.  
  19. int main()
  20. {
  21. std::cout << power(2, 5) << std::endl;
  22. std::cout << power(2.5, 3) << std::endl;
  23. std::cout << power(2, -5) << std::endl;
  24. std::cout << power(-1., 200000) << std::endl;
  25. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
32
15.625
0.03125
1