C++ std::erf() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

In C++, std::erf(x) calculates the error function. It is a smooth S-shaped curve that starts at -1 for very negative numbers, passes through 0 at x = 0, and rises toward 1 for large numbers.

The function comes from statistics. It tells you how much of a bell curve (the normal distribution) lies near the middle. With it you can find probabilities such as "how many values fall within two standard deviations of the average". You do not need to know the formula to use it.

std::erf() is declared in <cmath> and is part of the group Special functions. Special functions appear in statistics and science. They are ready to use in <cmath>, so you do not need to write them yourself.

Syntax

Include <cmath> and call std::erf. Start with these lines.

C++
double a = std::erf(0.0);     // 0
double b = std::erf(1.0);     // 0.842701
double c = std::erf(-1.0);    // -0.842701
CallWhat it does
std::erf(0.0)The middle of the curve. Returns 0.
std::erf(1.0)Returns about 0.842701.
std::erf(-1.0)The function is odd, so the answer is the same with a minus sign: -0.842701.

There is no domain error for erf(). Every real number is allowed, and the result always stays between -1 and 1.

Library declarations
C++
float       erf(float x);
double      erf(double x);
long double erf(long double x);
double      erf(Integer x);   // for integer types

Parameters

ParameterDescription
xRequired. The number to evaluate. It can be float, double, long double, or an integer type (converted to double).

Return Value

Return ValueDescription
Value in [-1, 1]The error function of x. It is 0 for x = 0 and gets closer to 1 as x grows.
+1 / -1Returned for +infinity and -infinity. For very large x the result already rounds to 1.
NaNReturned if x is NaN.

C++ Examples

Example 1 — Values of the Error Function

Print erf() for negative, zero, and positive numbers to see the S-shaped curve in numbers.

C++
#include <cmath>
#include <iostream>

int main() {
    double values[] = {-1.0, 0.0, 0.5, 1.0, 2.0, 3.0};

    for (double x : values) {
        std::cout << "erf(" << x << ") = " << std::erf(x) << '\n';
    }
    return 0;
}

How It Works

  1. The loop calls erf() on six numbers and prints each answer with the default six digits.
  2. erf(0) is exactly 0. For -1 and 1 the answers are -0.842701 and 0.842701, the same size with opposite signs.
  3. The values grow quickly toward 1: 0.5205 for 0.5, 0.995322 for 2, and 0.999978 for 3.

Example 2 — Symmetry and the Limit of 1

Show that erf(-x) is the negative of erf(x), and that large inputs reach 1.

C++
#include <cmath>
#include <iomanip>
#include <iostream>

int main() {
    std::cout << std::setprecision(12);

    std::cout << "erf(2) + erf(-2) = " << std::erf(2.0) + std::erf(-2.0) << '\n';
    std::cout << "erf(5)  = " << std::erf(5.0) << '\n';
    std::cout << "erf(10) = " << std::erf(10.0) << '\n';
    return 0;
}

How It Works

  1. With setprecision(12) we can see twelve digits, enough to watch the value get close to 1.
  2. erf(2) is 0.995322... and erf(-2) is the same with a minus sign, so they add up to exactly 0.
  3. erf(5) is 0.999999999998, already very close to 1. By erf(10) the difference from 1 is too small for a double, so it prints 1.

Example 3 — The 68-95-99.7 Rule

For a normal distribution, the share of values within k standard deviations of the average is erf(k / sqrt(2)). Print it as a percentage.

C++
#include <cmath>
#include <iomanip>
#include <iostream>

int main() {
    std::cout << std::fixed << std::setprecision(2);

    for (int k = 1; k <= 3; ++k) {
        double share = std::erf(k / std::sqrt(2.0));
        std::cout << "within " << k << " std dev: " << share * 100 << "%\n";
    }
    return 0;
}

How It Works

  1. The bell curve is measured in standard deviations. Dividing k by sqrt(2) turns it into the number that erf() expects.
  2. For k = 1 the result is 0.6827, so about 68.27% of the values lie within one standard deviation of the average.
  3. For k = 2 it is 95.45% and for k = 3 it is 99.73%. These are the numbers behind the well-known 68-95-99.7 rule.

Frequently Asked Questions

std::erf(x) returns the error function of x. The result goes from -1 to 1, is 0 at x = 0, and gets very close to 1 for large x.
The error function of x, a number from -1 to 1. erf(0) is 0, erf(+inf) is 1, and erf(-inf) is -1.
Mostly for statistics. erf() gives the area under a normal bell curve, so you can find probabilities, confidence levels, and p-values.
Because the true value is so close to 1 that a double cannot tell the difference. Use erfc(x) when you need the tiny value 1 - erf(x).
Include <cmath> and write std::erf(). That always works and picks the C++ overloads. The global name ::erf often works too, but the standard does not promise it, and it may only take a double.

Next: erfc()

Keep going through the <cmath> functions. The complementary error function, 1 - erf(x), kept accurate.

erfc() tutorial →

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.

11 people found this page helpful