What is erf in C?
The erf function is defined in the <math.h> header file in C. It takes in a single parameter: a double value. It then computes the error function of the given argument and returns a value of type double.
The
erf =
The argument to the
erffunction serves as the upper 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 illustration below shows how erf function works:
Declaration
The erf function is defined as follows:
double erf(double arg);
It takes in a single value of type double and computes its error function. It then returns the answer of type double.
Example
The following code snippet shows how we can use the erf function:
#include <stdio.h> // Including header file for printf function#include <math.h> // Including header file for erf functionint main (){double param, result;param = 2.0;result = erf(param);printf("erf (%f) = %f\n", param, result);return 0;}
Error handling
The erf function returns special values for certain arguments:
- If the argument is ±0, ±0 is returned
- If the argument is ±, ± is returned
- If the argument is
NAN,NANis returned
Example
The following code snippet shows how error handling in the erf function works:
#include <stdio.h> // Including header file for printf function#include <math.h> // Including header file for erf functionint main (){printf("erf (%f) = %f\n", 0.0, erf(0.0));return 0;}
Free Resources