C++ std::erfc() Function

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

Definition

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
CallWhat 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

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 [0, 2]The value 1 - erf(x). It is 1 at x = 0 and falls toward 0 for large x.
0 / 2Returned for +infinity and -infinity. Very large x can also underflow to 0.
NaNReturned 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;
}

How It Works

  1. The loop prints erfc(x) for six values. It starts at 1 for x = 0.
  2. The answers fall quickly: 0.4795, 0.157299, 0.00467773, and 2.20905e-05 for 0.5, 1, 2, and 3.
  3. For -1 the value is 1.8427, which equals 1 - erf(-1) or 1 + 0.842701. Negative inputs give answers between 1 and 2.

Example 2 — 1 - erf(x) Loses Precision

Compare 1 - erf(x) with erfc(x) for three growing values of x.

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

int main() {
    for (double x : {4.0, 6.0, 8.0}) {
        std::cout << "x = " << x
                  << ": 1 - erf = " << 1.0 - std::erf(x)
                  << ", erfc = " << std::erfc(x) << '\n';
    }
    return 0;
}

How It Works

  1. Both columns should show the same number, because erfc(x) is defined as 1 - erf(x).
  2. At x = 4 they agree on the first digits, since the tail 1.5e-08 is still bigger than the rounding error of a double (about 1.1e-16).
  3. At x = 6 and x = 8, erf(x) rounds to exactly 1, so the subtraction gives 0. Only erfc() still reports the true tiny values.

Example 3 — Probability of a Large Value

For a standard normal distribution, the chance of a value above k is 0.5 * erfc(k / sqrt(2)). Compute it for four values of k.

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

int main() {
    for (double k : {1.0, 2.0, 3.0, 5.0}) {
        double p = 0.5 * std::erfc(k / std::sqrt(2.0));
        std::cout << "P(Z > " << k << ") = " << p << '\n';
    }
    return 0;
}

How It Works

  1. P(Z > k) means the chance that a normally distributed value is more than k standard deviations above the average.
  2. The chance of going above 1 is about 15.9%, above 2 is 2.3%, and above 3 is only 0.13%.
  3. 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.

Next: exp()

Keep going through the <cmath> functions. The number e raised to a power.

exp() 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.

12 people found this page helpful