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 error functionalso known as Gauss error function in mathematics is defined as follows:

erf zz = 2π\frac{2}{\sqrt{\pi}} 0arget2dt\int_{0}^{arg} e^{-t^2} dt

The argument to the erf function 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:

How does erf work?

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 function
int 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 ±\infty, ± \infty is returned
  • If the argument is NAN, NAN is 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 function
int main (){
printf("erf (%f) = %f\n", 0.0, erf(0.0));
return 0;
}

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved