Approximation of error-function erf(x) The code beyond approximates the error-function erf(x) with following formular \$erf(x)=1-(a_1t+a_2t^2+a_3t^3)e^{-x^2})\$ for \$x\geq0\$ inclusive the identity \$erf(-x)=-erf(x)\$.


Is it okay to end the program in the middle of a code with exit(EXIT_FAILURE)? I just tried to avoid it and came to following solution, where the program always ends with return 0 at the end of the main-programm: 
https://codepaste.net/ek8ed5 So the question is whats's the more correct way? 


(I hope a moderator can paste this code section in here please, because somehow I didn't make it work to show it as nicely formatted code, sorry!)


What else can I improve?


.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;math.h&gt;

#define p 0.47047
#define a_1 0.3480242
#define a_2 -0.0958798
#define a_3 0.7478556


double ErrorFunction(double x);

int main(void)
{
  double x = 0;

  do
  {
    printf("Enter any value x to show erf(x): ");

    if (scanf_s("%lf", &amp;x) != 1)
    {
      printf("error: invalid input\n");
      exit(EXIT_FAILURE);
    }
    else
    {
      printf("\n erf(x)=%f\n\n", ErrorFunction(x));
    }

  } while (x != 0);

  return 0;
}

double ErrorFunction(double x)
{
  double t = 0;

  if (x &lt; 0)
  {
    x = -x;
    t = 1 / (1 + p*x);
    return -(1 - (a_1*t + a_2*t*t + a_3*t*t*t)*exp(-(x*x)));
  }
  else
  {
    t = 1 / (1 + p*x);
    return 1 - (a_1*t + a_2*t*t + a_3*t*t*t)*exp(-(x*x));
  }
}
DRY. Actual computations are repeated twice, which is always a signal for improvement:

if (x &lt; 0) {
    t = 1 / (1 - p*x);
} else {
    t = 1 / (1 + p*x);
}

result = compute_the_formula(t, x);

if (x &lt; 0) {
    result = -result;
}
return result;

Try to cut down multiplications. Instead of 

a_1*t + a_2*t*t + a_3*t*t*t


consider 

t*(a_1 + t*(a2 + t*a_3)))


Three multiplications instead of original six is not only faster, but generally more accurate.