Question: Is there a way to easily compute [the erf function](https://en.wikipedia.org/wiki/Normal_distribution#Cumulative_distribution) (or the cumulative distribution function of the normal law) in LaTeX?

Currently, I use `pgf` to make computation, but I did not find a way to compute erf using `pgf`.

I would be happy to use any package that is available to compute erf, or any custom solution to compute that function.

Answer: 
For precise values, I recommend externalizing the calculation, here `gnuplot` is used.

Code (needs `--shell-escape` enabled)
-------------------------------------

```
\documentclass{article}
\usepackage{amsmath,pgfmath,pgffor}
\makeatletter
\def\qrr@split@result#1 #2\@qrr@split@result{\edef\erfInput{#1}\edef\erfResult{#2}}
\newcommand*{\gnuplotErf}[2][\jobname.eval]{%
    \immediate\write18{gnuplot -e "set print '#1'; print #2, erf(#2);"}%
    \everyeof{\noexpand}
    \edef\qrr@temp{\@@input #1 }%
    \expandafter\qrr@split@result\qrr@temp\@qrr@split@result
}
\makeatother
\DeclareMathOperator{\erf}{erf}
\begin{document}
\foreach \x in {-50,...,50}{%
\pgfmathparse{\x/50}%
\gnuplotErf{\x/50.}%
$ x = \pgfmathresult = \erfInput, \erf(x) = \erfResult$\par
}
\end{document}

```

Output
------

![enter image description here](https://i.stack.imgur.com/hThhI.png)
You can also use the FFI in LuaJITTeX (and LuaTeX ≥ 1.0.3) to access the `erf` function in `libm` directly. The performance and accuracy are simply amazing!

```
\documentclass{article}

\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\directlua{
local ffi = require("ffi")
ffi.cdef[[
double erf(double arg);
]]
}

\pgfmathdeclarefunction{erf}{1}{%
  \edef\pgfmathresult{%
    \directlua{tex.print(ffi.C.erf(\pgfmathfloatvalueof{#1}))}%
  }%
}

\DeclareMathOperator\erf{erf}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    no marks, samples=101,
    xlabel={$x$},
    ylabel={$\erf(x)$},
    ]
    \addplot {erf(x)};
  \end{axis}
\end{tikzpicture}

\end{document}

```

[![enter image description here](https://i.stack.imgur.com/GLYdM.png)](https://i.stack.imgur.com/GLYdM.png)