   POW function  optmization question
  I'm trying to write my own optimized POW function that takes 2 floats as parameters one is the base number and the other is the exponent. I can't think of any better way than manually multiplying the number iteratively (e.g for 2^5 I am manually doing 2*2*2*2*2 but I'm doing it in a loop). Is there a faster solution (maybe with some kind of bit shifting)?   
  There are much, much, (much!) faster ways...  Plus your method doesn't work at all for fractional exponents.  Some good google terms might be "series expansion", recurrence, iteration.  There's lots of material out there depending on how much math you know.I really doubt you'll be able to beat the hardware FPU though.   ;  well, you said your pow takes two floats, so loops may not work (can loop only a whole number of iterations)if you change your pow to take a whole number for the exponent there is a simple optimization:for powers of 2 you can repeatedly squareexample:  x^64 = ((((((x^2)^2)^2)^2)^2)^2)   (6 loop iterations instead of 64)for a number that isnt a power of two you can break it down to powers of two (write the exponent in binary) and then do each p-o-2 seperatly and then multiply together.edit: forgot an example!example:  x^40 = x^(32+8) = x^32 * x^8  = ((((x^2)^2)^2)^2)^2  * (((x^2)^2)^2)(8 iterations instead of 40).here is a code of the above trickery,note: might be buggy, just wrote here without testing.int exponenet;float x;int pot = 0;float result=1;while (exponent>0){  digit = exponent & 1;  // get the right most binary digit  if (digit!=0) {    float temp = x;    for (int c=pot; c>0; c--)  // square x the power-of-two times  temp *= temp;    result = result * temp;  }  pot++;    // increase power of two  exponent = exponent >> 1; // shift right}return result;Iftah.[Edited by - Iftah on December 6, 2005 10:30:01 PM];  assuming at the moment that we are not using fractional exponents is there any faster way other than iteration to increase a base number by a whole number exponent? I know that you can do cool things with bit shifting but I'm not sure how or if it can even be used in this case   ;  bit shifting works great if you know that the base will be 2 :2^y = 1<<ybut as Anon Mike said, why do you write such a function?use the pow function that comes with the compiler (in math lib i think).[Edited by - Iftah on December 7, 2005 2:48:40 AM];  Ok, granted, this doesn't quite match what you want, since raising a number to some power QUICKLY overflows any integer, and the following code is designed for integers not floats. But the algoritm that is described above is the "square and multiply" algorithm.  In a homework program written for my cryptography class, we used it to quickly raise a number to some very large power, BUT it takes a limited working field (N), and gives youA^B mod N.int sqpow ( int base, int exp, int n ){cout<<"  square power:\n";vector<bool> bits;for ( int cnt1 = 0; cnt1 < sizeof(int)*8; cnt1++ ){bool bit = exp & 1;exp = exp >> 1;bits.push_back(bit);}int rVal = 1;while ( bits.back() == 0 ) bits.pop_back();int sz = bits.size();for ( int cnt1 = 0; cnt1 < sz; cnt1++ ){cout<<(bits[cnt1]?"1":"0");}cout<<endl;    cout<<base<<"=>\n";for ( int cnt1 = 0; cnt1 < sz; cnt1++ ){bool bit = bits.back();bits.pop_back();   if ( bit ){rVal *= base;}else{rVal *= 1;}rVal %= n;if ( cnt1 != sz -1 ){   rVal *= rVal;}cout<<rVal<<" mod "<<n<<" = ";rVal %= n;cout<<rVal<<" \n";}return rVal;}It takes the power and breaks it into a binary representation (The above code does this REALY REALY slow compared to what you can get out of straight bitshifts)Then depending on if a bit is a 1 or a 0 you either multiply by base and square, or just square.This can be adapted to work on floats (you need to remove the mod stuff, and a couple other changes)--Edit, a bit slow, some other code that is similar got posted above ^^x^40 = X^(101000) -> (((((((x)^2)*1)^2)*x)^2)^2)^2)  6 itterations instead of 40   ;  oops, thanks KulSeran, its been a while since i studied that algo,my implementation is abit slower since for example i start calculating x^32 from scratch even if i had x^16 from the previous bit (the squaring if the bit is zero is the trick i forgot).true it will overflow any integer fast, so not useful for integer power without modulo,but he said his base can be float so it can be small numbers, ie:  1.1^60Iftah.   