In C++, std::erfc(x) is the complementary error function. It is simply 1 - erf(x), so erfc(0) is 1, and the curve falls toward 0 as x grows.
Why have a separate function? For big x, erf(x) is so close to 1 that subtracting it from 1 throws away every digit. erfc() avoids that subtraction, so the tiny tail value is still correct. This matters for very small probabilities.
std::erfc() 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::erfc. Start with these lines.
C++
double a = std::erfc(0.0); // 1
double b = std::erfc(1.0); // 0.157299
double c = std::erfc(-1.0); // 1.8427
Call
What it does
std::erfc(0.0)
1 - erf(0) is 1 - 0. Returns 1.
std::erfc(1.0)
Returns about 0.157299, which is 1 - 0.842701.
std::erfc(-1.0)
For negative x the answer is above 1: 1.8427.
There is no domain error for erfc(). The result stays between 0 and 2.
Library declarations
C++
float erfc(float x);
double erfc(double x);
long double erfc(long double x);
double erfc(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 [0, 2]
The value 1 - erf(x). It is 1 at x = 0 and falls toward 0 for large x.
0 / 2
Returned for +infinity and -infinity. Very large x can also underflow to 0.
NaN
Returned if x is NaN.
C++ Examples
Example 1 — Values of the Complementary Error Function
Print erfc() for zero, small, large, and negative numbers.
C++
#include <cmath>
#include <iostream>
int main() {
double values[] = {0.0, 0.5, 1.0, 2.0, 3.0, -1.0};
for (double x : values) {
std::cout << "erfc(" << x << ") = " << std::erfc(x) << '\n';
}
return 0;
}
P(Z > k) means the chance that a normally distributed value is more than k standard deviations above the average.
The chance of going above 1 is about 15.9%, above 2 is 2.3%, and above 3 is only 0.13%.
At k = 5 the probability is about 2.9e-07, roughly one in 3.5 million. Only erfc() can give such a small number reliably.
Frequently Asked Questions
std::erfc(x) returns the complementary error function, which is 1 - erf(x). It stays accurate for large x, where 1 - erf(x) would lose all its digits.
The value 1 - erf(x), a number from 0 to 2. erfc(0) is 1, erfc(+inf) is 0, and erfc(-inf) is 2.
erfc(x) equals 1 - erf(x). The two are mathematically linked, but erfc() stays accurate for large x, while 1 - erf(x) loses its digits.
Use it for tail probabilities, very small chances, and any time you would write 1 - erf(x) with a large x.
Include <cmath> and write std::erfc(). That always works and picks the C++ overloads. The global name ::erfc often works too, but the standard does not promise it, and it may only take a double.