title: self made pow() c++
body: <p>I was reading through <a href="https://stackoverflow.com/questions/2882706/how-can-i-write-a-power-function-myself/2882819#2882819">How can I write a power function myself?</a> and the answer given by dan04 caught my attention mainly because I am not sure about the answer given by fortran, but I took that and implemented this:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  float pow(float base, float ex){
      // power of 0
      if (ex == 0){
          return 1;
      // negative exponenet
      }else if( ex &lt; 0){
          return 1 / pow(base, -ex);
      // even exponenet
      }else if ((int)ex % 2 == 0){
          float half_pow = pow(base, ex/2);
          return half_pow * half_pow;
      //integer exponenet
      }else{
          return base * pow(base, ex - 1);
      }
  }
  int main(){
      for (int ii = 0; ii&lt; 10; ii++){\
          cout &lt;&lt; "pow(" &lt;&lt; ii &lt;&lt; ".5) = " &lt;&lt; pow(ii, .5) &lt;&lt; endl;
          cout &lt;&lt; "pow(" &lt;&lt; ii &lt;&lt; ",2) = " &lt;&lt; pow(ii,  2) &lt;&lt; endl;
          cout &lt;&lt; "pow(" &lt;&lt; ii &lt;&lt; ",3) = " &lt;&lt; pow(ii,  3) &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>though I am not sure if I translated this right because all of the calls giving .5 as the exponent return 0. In the answer it states that it might need a log2(x) based on <code>a^b = 2^(b * log2(a))</code>, but I am unsure about putting that in as I am unsure where to put it, or if I am even thinking about this right.</p>
  
  <p>NOTE: I know that this might be defined in a math library, but I don't need all the added expense of an entire math library for a few functions.</p>
  
  <p>EDIT: does anyone know a floating-point implementation for fractional exponents? (I have seen a double implementation, but that was using a trick with registers, and I need floating-point, and adding a library just to do a trick I would be better off just including the math library)</p>