Writing a while loop for error function erf(x)?
<p>I understand there is a <a href="https://docs.python.org/3/library/math.html#math.erf" rel="nofollow">erf</a> (<a href="https://en.wikipedia.org/wiki/Error_function" rel="nofollow">Wikipedia</a>) function in python. But in this assignment we are specifically asked to write the error function as if it was not already implemented in python while using a <code>while loop</code>. </p>

<p><code>erf (x)</code> is simplified already as : <code>(2/ (sqrt(pi)) (x - x^3/3 + x^5/10 - x^7/42...)</code></p>

<p>Terms in the series must be added until the absolute total is less than 10^-20.</p>

<p>First of all - SO is not the place when people code for you, here people help you to solve particular problem not the whole task </p>

<p>Any way:
It's not too hard to implement wikipedia algorithm: </p>

<pre><code>import math


def erf(x):
    n = 1
    res = 0
    res1 = (2 / math.sqrt(math.pi)) * x
    diff = 1
    s = x
    while diff &gt; math.pow(10, -20):
        dividend = math.pow((-1), n) * math.pow(x, 2 * n + 1)
        divider = math.factorial(n) * (2 * n + 1)
        s += dividend / divider
        res = ((2 / math.sqrt(math.pi)) * s)
        diff = abs(res1 - res)
        res1 = res
        n += 1
    return res


print(erf(1))
</code></pre>

<p>Please read the source code carefully and post all questions that you don't understand. </p>

<p><a href="http://svn.python.org/view/python/trunk/Modules/mathmodule.c?view=markup" rel="nofollow">Also you may check python sources</a> and see how erf is implemented </p>

python while-loop