Note that there are some explanatory texts on larger screens.

plurals
  1. POe to the power x in C
    text
    copied!<p>I am trying to find e to the power x. Please indicate what could possibly be wrong with my current implementation rather than simply suggesting new/ore efficient solutions (i could find many on the net). There seems to be a logical or run time error, debugging it doesn't show anything. Thanks in advance!</p> <p>cmath library has been included, program compiles fine..stops on run</p> <pre><code>double find_e_power_x(int x){ int i = 1, j = 1, count = 1, accuracy = 15; double xd = static_cast&lt;double&gt;(x); //pow takes double args double jd = static_cast&lt;double&gt;(j); double epx = 1.0; while ( count &lt; accuracy ){ while ( ++i &amp;&amp; ++j) { epx += ( pow(xd,jd) / fact(i) ); //static_cast count++; } } return epx; } </code></pre> <p>In response to the comments (pointing out my infinite inner loop), </p> <p><strong>EDIT:</strong></p> <pre><code>while ( count &lt; accuracy ){ epx += ( pow(xd,jd) / fact(i) ); //static_cast count++; i++; j++; } </code></pre> <p>input =3 answer is incorrect though it does give an output as 3.15485</p> <hr> <p>below is the <strong>final version</strong> works fine </p> <p>i/p = 4 </p> <p>o/p = 54.8278</p> <pre><code>double find_e_power_x(int x){ int i = 1, count = 1, accuracy = 15; double xd = static_cast&lt;double&gt;(x); //pow takes double args double id = static_cast&lt;double&gt;(i); double epx = 1.0; while ( count &lt; accuracy ){ epx += ( pow(xd,id) / fact(i) ); //static_cast ++count; ++id; ++i; } return epx; } </code></pre>
 

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