Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think the algorithm you're looking for could be '<a href="http://en.wikipedia.org/wiki/Nth_root_algorithm" rel="nofollow">nth root</a>'. With an initial guess of 1 (for k == 0):</p> <pre><code>#include &lt;iostream&gt; using namespace std; float pow(float base, float ex); float nth_root(float A, int n) { const int K = 6; float x[K] = {1}; for (int k = 0; k &lt; K - 1; k++) x[k + 1] = (1.0 / n) * ((n - 1) * x[k] + A / pow(x[k], n - 1)); return x[K-1]; } float pow(float base, float ex){ if (base == 0) return 0; // power of 0 if (ex == 0){ return 1; // negative exponenet }else if( ex &lt; 0){ return 1 / pow(base, -ex); // fractional exponent }else if (ex &gt; 0 &amp;&amp; ex &lt; 1){ return nth_root(base, 1/ex); }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_pow(int, char **){ 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; } return 0; } </code></pre> <p>test:</p> <pre><code>pow(0, .5) = 0.03125 pow(0, 2) = 0 pow(0, 3) = 0 pow(1, .5) = 1 pow(1, 2) = 1 pow(1, 3) = 1 pow(2, .5) = 1.41421 pow(2, 2) = 4 pow(2, 3) = 8 pow(3, .5) = 1.73205 pow(3, 2) = 9 pow(3, 3) = 27 pow(4, .5) = 2 pow(4, 2) = 16 pow(4, 3) = 64 pow(5, .5) = 2.23607 pow(5, 2) = 25 pow(5, 3) = 125 pow(6, .5) = 2.44949 pow(6, 2) = 36 pow(6, 3) = 216 pow(7, .5) = 2.64575 pow(7, 2) = 49 pow(7, 3) = 343 pow(8, .5) = 2.82843 pow(8, 2) = 64 pow(8, 3) = 512 pow(9, .5) = 3 pow(9, 2) = 81 pow(9, 3) = 729 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload