Python math.erf() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Python 3.2+

Definition

math.erf(x) returns the Gauss error function of x as a float between -1.0 and 1.0.

Added in Python 3.2, it lets you compute Gaussian (normal) probabilities directly without external libraries: the probability within z standard deviations of the mean is math.erf(z / math.sqrt(2)). For extreme tails near 1.0, prefer math.erfc().

math.erf() is a function in Python's standard math module: import it with import math and call math.erf(...), or import the name directly with from math import erf.

Syntax

Import math and pass a real number.

python
import math

prob = math.erf(x)
CallWhat it does
math.erf(0)Returns 0.0.
math.erf(1)Returns approximately 0.842701....
math.erf(float("inf"))Returns 1.0.
math.erf(float("-inf"))Returns -1.0.

Notice math.erf() is an odd function: math.erf(-x) == -math.erf(x). Use math.erfc() for 1 - erf(x) when x > 4.

Function signatures
python
math.erf(x, /)
# added in Python 3.2; returns float error function in [-1.0, 1.0]

Parameters

ParameterDescription
xRequired positional real number (int or float).

Return Value

Return ValueDescription
floatValue in [-1.0, 1.0]; 1.0 for inf, -1.0 for -inf, nan for nan.

Python Examples

Example 1 — Verify the 68-95-99.7 Normal Distribution Rule

Calculate the share of normal observations falling within one, two, and three standard deviations.

python
import math

for sigma in [1, 2, 3]:
    share = math.erf(sigma / math.sqrt(2.0))
    print(f"+/-{sigma} sigma -> {share:.4f}")

How It Works

  1. Convert sigma to erf scale. Dividing standard deviation count by math.sqrt(2.0) maps directly into the Gaussian error integral.
  2. Classic empirical rule. Output shows 68.27%, 95.45%, and 99.73% coverage.
  3. Four-decimal stability. Formatting with :.4f keeps output clean across platforms.

Example 2 — Build a Standard Normal CDF With math.erf()

Compute the cumulative probability P(Z <= z) = 0.5 * (1 + erf(z / sqrt(2))).

python
import math


def normal_cdf(z):
    return 0.5 * (1.0 + math.erf(z / math.sqrt(2.0)))


for z in [-1.0, 0.0, 1.0, 2.0]:
    cdf = normal_cdf(z)
    print(f"z={z:+4.1f} -> CDF={cdf:.4f}")

How It Works

  1. Symmetric midpoint. At z = 0.0, erf(0) is zero, returning exact 0.5000.
  2. Complementary pairs. Probabilities at -1.0 and +1.0 sum cleanly to one.
  3. No external packages. Uses only math.erf() and math.sqrt() from the standard library.

Example 3 — Check Odd Symmetry, Rounded erf(1), and Infinity

Confirm odd reflection erf(-1) == -erf(1) and check infinite limits.

python
import math

print(math.erf(0), f"{math.erf(1):.6f}")
print(math.erf(-1.0) == -math.erf(1.0))
print(math.erf(float("inf")), math.erf(float("-inf")))

How It Works

  1. Zero passes through zero. Passing 0 produces 0.0, and erf(1) formats to 0.842701.
  2. Exact odd reflection. Negating the input negates the result cleanly.
  3. Finite limits at infinity. Positive and negative infinity saturate at 1.0 and -1.0.

Frequently Asked Questions

Python 3.2.
Use 0.5 * (1.0 + math.erf((x - mean) / (std * math.sqrt(2.0)))).
True erf(6.0) is about 1 - 2.15e-17, which is closer to 1.0 than the nearest distinct float below 1.0. Use math.erfc(6.0) for the tail.
1.0, and math.erf(float("-inf")) returns -1.0.
Yes, it preserves -0.0 like most IEEE 754 odd functions.

Next: math.erfc()

Return the complementary error function 1 - erf(x) accurately.

erfc() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful