What is erfc in C?
The erfc function is defined in the math.h header file in C. It takes in a single parameter: a double value. It then computes the complementary error function of the given argument and returns a type double value.
The complementary error function in mathematics is defined as follows:
erfc =
=
The argument to the
erfcfunction serves as the lower limit in the integral above.
The error function is most often used in probability and statistics. It integrates the normal distribution and gives the probability that a normally distributed random variable Y (with mean 0 and variance ½) falls into the range [−x, x].
The complementary error function, combined with its series expansion, provides approximate expressions for small and large values of x.
The figure below shows the plot of the erfc function:
The illustration below shows how erfc function works:
Declaration
The erfc function is defined as follows:
double erfc(double arg);
It takes in a single value of type double and computes its complementary error function. It then returns the answer of type double.
Error handling
The erfc function returns special values for certain arguments:
| Special Value | Returned |
|---|---|
| +INFINITY | +0 |
| -INFINITY | +2 |
NAN |
NAN |
Examples
The following code snippet shows how we can use the erfc function:
#include <stdio.h> // Including header file for printf function#include <math.h> // Including header file for erfc functionint main (){double param, result;param = 2.0;result = erfc(param);printf("erfc (%f) = %f\n", param, result);return 0;}
The following code snippet shows how error handling in erfc function works:
#include <stdio.h> // Including header file for printf function#include <math.h> // Including header file for erfc functionint main (){printf("erfc (%f) = %f\n", INFINITY, erfc(INFINITY));printf("erfc (%f) = %f\n", -INFINITY, erfc(-INFINITY));return 0;}
Free Resources