How to Compute the Power of Arbitrary Base without Loops in C/C++?


If we want to compute tex_b248065f5e10c6e8f286437497dc56fe How to Compute the Power of Arbitrary Base without Loops in C/C++? c / c++ math , and if the y is integer, we can easily do this using a straigtforward loop O(n), or a O(logn) approach. However, if the exponential is a double, this cannot be done in this manner. However, with some mathematics proof, we can do it using just two functions, the exp to compute the tex_474f9eefecc087e5804b33cc66a0b65e How to Compute the Power of Arbitrary Base without Loops in C/C++? c / c++ math and log which computes the tex_66c0d96443c31f57d42711aab090f188 How to Compute the Power of Arbitrary Base without Loops in C/C++? c / c++ math .

tex_d36b720a8227cbe9beacd1e03530d996 How to Compute the Power of Arbitrary Base without Loops in C/C++? c / c++ math after wrapped with the ln function it becomes:

tex_e2574b66cf15acb41c35c7f13a8528df How to Compute the Power of Arbitrary Base without Loops in C/C++? c / c++ math and we continue by adding the exp wrapper, which becomes:

tex_f6085d2723a9c59a6cdde84853d86bbe How to Compute the Power of Arbitrary Base without Loops in C/C++? c / c++ math is simplified to:

tex_2281d89abda948f55d0681a5efd3d607 How to Compute the Power of Arbitrary Base without Loops in C/C++? c / c++ math there you go, with the simple/effective formula.

power-x-y-formula How to Compute the Power of Arbitrary Base without Loops in C/C++? c / c++ math

power-x-y-formula

It can be easily implemented in C/C++ that includes the math.h:

1
2
3
4
inline 
double power(double x, double y) {
    return exp(y * log(x));
}
inline 
double power(double x, double y) {
    return exp(y * log(x));
}

This article just gives you the quick implementation if you just want to know roughly the value of tex_b248065f5e10c6e8f286437497dc56fe How to Compute the Power of Arbitrary Base without Loops in C/C++? c / c++ math . However, it depends on how the functions exp and log are implemented in the system library math.h but these are quite standard and are defined and well implemented in ANSI C/C++, so there is nothing to worry about.

How is it compared to the pow() function in C/C++? Is it faster? Is it numerically stable? We have to run some tests before coming to a conclusion. The implementation of pow() may look like this:

1
2
3
4
5
6
7
8
9
double pow(double x, double y) {
   if (abs((int)y - y) < EPS) { // if y is integer
      return power(x, (int)y) ; // speed up if y is integer
   }
   if (y < 0) {
      return 1.0 / power(x, -y); // e.g. 2^(-1) = 1.0/(2^1)
   }
   return power(x, y); // use the above implementation :)
}
double pow(double x, double y) {
   if (abs((int)y - y) < EPS) { // if y is integer
      return power(x, (int)y) ; // speed up if y is integer
   }
   if (y < 0) {
      return 1.0 / power(x, -y); // e.g. 2^(-1) = 1.0/(2^1)
   }
   return power(x, y); // use the above implementation :)
}

--EOF (The Ultimate Computing & Technology Blog) --

GD Star Rating
loading...
666 words
Last Post: How to Read File Content from URL with Time out in PHP?
Next Post: How to Compute Minkowski, Euclidean and CityBlock Distance in C++?

The Permanent URL is: How to Compute the Power of Arbitrary Base without Loops in C/C++?

2 Comments

  1. Gaurav Verma

Leave a Reply