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 ?