2

Exponentiation in most modern languages is easy .. I use the common operator for that in my language of choice or whatever function that compensates that to get the desired functionality.
I want to know, how does this exactly work ?

The following algorithm in C is often used to demonstrate this effect ..

double exp(val, pow) {
    for(int i = 0; i < pow; ++i)
        val *= val;
    return val;
} // exp(2, 3) -> 8

However, there is a serious bug here .. What if pow is 2.6 ? That would return 8 also ..
That's simply because the loop condition only compares the two numbers ..
But when I do something like this, it works well ..

#include <math.h>
int main() {
    printf("The result of 2 to the power of 2.6 is %.2f", pow(2, 2.6));
}

How can the latter behavior be achieved ?

Edit:

According to the answers, it seems the taylor expansion algorithm is the key to exponentiation, so .. what about multiplication ? How can decimal multiplication be achieved ?

2

2 Answers 2

8

Exponentiation is usually implemented as (lots of special cases plus) a reduction to exp. If you have an exp function and its inverse ln handy, you can compute x^y as

exp(y*ln(x))

But you might wonder how exp is implemented. For small arguments, the series expansion works well:

exp(x) = 1 + x + x^2/2 + x^3/6 + x^4/24 + ...

Edit: This is the Taylor expansion referred to in the other answers.

For larger values there are argument reduction techniques that can be used to compute the value.

4
  • 1
    Certain platforms can take advantage of hardware as well. i.e. x86 can use the x87 FPU Aug 5, 2014 at 18:27
  • 5
    AFAIK, exp (and some other transcendental functions) is often computed using chebyshev polynomials, not Taylor expensions. BTW, some compilers translate exp (on x86-64) to the hardware instruction computing it. Aug 5, 2014 at 18:27
  • @DrewMcGowen: Absolutely. I thought that would be out of scope of the answer, though.
    – Charles
    Aug 5, 2014 at 18:31
  • @BasileStarynkevitch: Yes, hardware instructions are almost always the way to go.
    – Charles
    Aug 5, 2014 at 18:38
1

There isn't a way to accurately solve this problem but it is possible to approximate with a series as seen here.

5
  • 1
    Please try and avoid link-only answers. Include the relevant information here, as links have a tendency to change. Aug 5, 2014 at 18:26
  • @MikePrecup the link is to a StackOverflow answer - those kind of "internal links" seldom change in my experience. Aug 5, 2014 at 18:28
  • @MortenJensen My bad, I hit the wrong macro, meant to use the internal links one. Link only answers are still considered bad, though, according to site standards, when no other information is provided. Aug 5, 2014 at 18:32
  • @MikePrecup I disagree with you in that the solution is complex enough that we should strive to keep them in a community wiki or something. Trig approximations in C are notoriously difficult (and bug prone!) to write yourself :) Aug 5, 2014 at 18:34
  • @MortenJensen Hm, the community wiki is an interesting point. Fair enough about the difficulty, shall we delete this conversation in a few minutes to remove some noise from the page? Aug 5, 2014 at 18:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.