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 ?

Community
  • 1
  • 1
Amr Ayman
  • 1,129
  • 1
  • 8
  • 24

2 Answers2

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.

Charles
  • 11,269
  • 13
  • 67
  • 105
  • 1
    Certain platforms can take advantage of hardware as well. i.e. x86 can use the x87 FPU – Drew McGowen Aug 05 '14 at 18:27
  • 5
    AFAIK, `exp` (and some other transcendental functions) is often computed using [chebyshev polynomials](http://en.wikipedia.org/wiki/Chebyshev_polynomials), not Taylor expensions. BTW, some compilers translate `exp` (on x86-64) to the hardware instruction computing it. – Basile Starynkevitch Aug 05 '14 at 18:27
  • @DrewMcGowen: Absolutely. I thought that would be out of scope of the answer, though. – Charles Aug 05 '14 at 18:31
  • @BasileStarynkevitch: Yes, hardware instructions are almost always the way to go. – Charles Aug 05 '14 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.

Community
  • 1
  • 1
Jaden Travnik
  • 1,107
  • 13
  • 27
  • 1
    Please try and avoid link-only answers. Include the relevant information here, as links have a tendency to change. – Mike Precup Aug 05 '14 at 18:26
  • @MikePrecup the link is to a StackOverflow answer - those kind of "internal links" seldom change in my experience. – Morten Jensen Aug 05 '14 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. – Mike Precup Aug 05 '14 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 :) – Morten Jensen Aug 05 '14 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? – Mike Precup Aug 05 '14 at 18:38