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
Call
What 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
Parameter
Description
x
Required. The number to evaluate. It can be float, double, long double, or an integer type (converted to double).
Return Value
Return Value
Description
Value in [-1, 1]
The error function of x. It is 0 for x = 0 and gets closer to 1 as x grows.
+1 / -1
Returned for +infinity and -infinity. For very large x the result already rounds to 1.
NaN
Returned 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;
}
within 1 std dev: 68.27%
within 2 std dev: 95.45%
within 3 std dev: 99.73%
How It Works
The bell curve is measured in standard deviations. Dividing k by sqrt(2) turns it into the number that erf() expects.
For k = 1 the result is 0.6827, so about 68.27% of the values lie within one standard deviation of the average.
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.