erf function

double erf(
  1. num x
)

Returns an approximation of the error function, for details see https://en.wikipedia.org/wiki/Error_function.

This uses a Chebyshev fitting formula from Numerical Recipes, 6.2.

Implementation

double erf(num x) {
  const p = [
    -1.26551223,
    1.00002368,
    0.37409196,
    0.09678418,
    -0.18628806,
    0.27886807,
    -1.13520398,
    1.48851587,
    -0.82215223,
    0.17087277,
  ];
  final t = 1.0 / (1.0 + 0.5 * x.abs());
  final e = -x * x + p.polynomial(t);
  final r = t * exp(e);
  return x.isNegative ? r - 1.0 : 1.0 - r;
}