SciPy error function: erf, erfc (complementary) and erfinv (inverse), tested
The error function lives in scipy.special: erf(x), the complementary erfc(x) (use it instead of 1 - erf(x) for large x) and the inverse erfinv(y) for y in [-1, 1]. np.erf does not exist and math.erf rejects arrays.
1. erf, erfc, erfinv, erfcinv and the normal distribution
import math
import numpy as np
from scipy import special, stats
x = np.array([0.0, 0.5, 1.0, 2.0, 5.0])
print("erf ", special.erf(x))
print("math.erf ", [math.erf(v) for v in x])
print("erfc ", special.erfc(x))
print("1 - erf ", 1 - special.erf(x), "<- x=5: wrong from the 5th digit")
print("erfc(10) ", special.erfc(10.0), " 1-erf(10):", 1 - special.erf(10.0))
print("erfinv ", special.erfinv([0.0, 0.5, 0.95, 1.0, 1.5]))
print("erfcinv ", special.erfcinv([1.0, 0.5, 1e-20]))
# link to the normal distribution
z = 1.96
print("norm.cdf ", stats.norm.cdf(z), " 0.5*(1+erf(z/sqrt2)):", 0.5 * (1 + special.erf(z / np.sqrt(2))))
print("norm.ppf ", stats.norm.ppf(0.975), " sqrt2*erfinv(0.95):", np.sqrt(2) * special.erfinv(0.95))
print("complex ", special.erf(1 + 1j))
Output (SciPy 1.18.1, NumPy 2.5.4, Python 3.13.5):
erf [0. 0.52049988 0.84270079 0.99532227 1. ]
math.erf [0.0, 0.5204998778130465, 0.8427007929497149, 0.9953222650189527, 0.9999999999984626]
erfc [1.00000000e+00 4.79500122e-01 1.57299207e-01 4.67773498e-03
1.53745979e-12]
1 - erf [1.00000000e+00 4.79500122e-01 1.57299207e-01 4.67773498e-03
1.53743684e-12] <- x=5: wrong from the 5th digit
erfc(10) 2.0884875837625446e-45 1-erf(10): 0.0
erfinv [0. 0.47693628 1.38590382 inf nan]
erfcinv [-0. 0.47693628 6.60158062]
norm.cdf 0.9750021048517795 0.5*(1+erf(z/sqrt2)): 0.9750021048517796
norm.ppf 1.959963984540054 sqrt2*erfinv(0.95): 1.9599639845400538
complex (1.3161512816979477+0.19045346923783463j)
scipy.special.erfis vectorized and matchesmath.erfvalue by value (NumPy prints1.for 0.9999999999984626 at x=5 because of display rounding).- Complementary error function: use
scipy.special.erfc(x), not1 - erf(x). At x=5 the subtraction is already wrong from the 5th significant digit, and at x=10 it returns 0.0 whileerfc(10)gives 2.09e-45. - Inverse error function:
erfinv(y)is defined on [-1, 1];erfinv(1)isinfand outside the range you getnan(no exception, no warning in this run).erfcinvis the inverse oferfcand stays accurate for tiny arguments like 1e-20. - Normal distribution:
norm.cdf(z) = 0.5*(1 + erf(z/sqrt(2)))andnorm.ppf(p) = sqrt(2)*erfinv(2p - 1); both pairs agree to the last digit or two above. If you only need probabilities,scipy.stats.normis clearer. special.erfalso accepts complex input.
2. "has no attribute erf" and "only 0-dimensional arrays"
import math
import numpy as np
def show(label, fn):
try:
print(label, "->", fn())
except Exception as e:
print(label, "->", type(e).__name__ + ":", e)
show("np.erf ", lambda: np.erf(0.5))
show("math.erf(array) ", lambda: math.erf(np.array([0.5, 1.0])))
show("np.vectorize ", lambda: np.vectorize(math.erf)(np.array([0.5, 1.0])))
show("scipy.erf ", lambda: __import__("scipy").erf(0.5))
from scipy.special import erf
show("special.erf list", lambda: erf([0, 1, 2]))
Output:
np.erf -> AttributeError: module 'numpy' has no attribute 'erf'
math.erf(array) -> TypeError: only 0-dimensional arrays can be converted to Python scalars
np.vectorize -> [0.52049988 0.84270079]
scipy.erf -> AttributeError: Module 'scipy' has no attribute 'erf'
special.erf list -> [0. 0.84270079 0.99532227]
- NumPy has no
erf, and top-levelscipydoes not export it: import it fromscipy.special. math.erfworks on single floats only; on an array it raisesTypeError. Usescipy.special.erf(fast, vectorized) rather thannp.vectorize(math.erf), which is a Python loop.
Related: SciPy standard error (stats.sem), scipy minimize errors.
Frequently asked questions
How do I compute the error function in Python?
from scipy.special import erf; erf(x) works on scalars and arrays. math.erf works on single floats.
What is the complementary error function in SciPy?
scipy.special.erfc(x) = 1 - erf(x), computed accurately even when erf(x) is close to 1.
What is the inverse error function in SciPy?
scipy.special.erfinv(y) for -1 <= y <= 1; it returns inf at 1 and nan outside the range.
Written with AI assistance. Measured on 2026-10-23 with SciPy 1.18.1, NumPy 2.5.4 and Python 3.13.5 on Linux. Scripts ran verbatim and the output was pasted unedited. PlainTables is our extension; SciPy is free.