What is erff in C?

The erff function is defined in the <math.h> header file in C. It takes in a single parameter: a float value. It then computes the error function of the given argument and returns a value of type float.

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 erff function serves as the upper limit in the integral above.

The error function is widely 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 erff function works:

How does erff work?

Declaration

The erff function is defined as follows:

float erff(float arg);

It takes in a single value of type float and computes its error function. It then returns the answer of type float.

Error handling

The erff 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

Examples

The following code snippet shows how we can use the erff function:

#include <stdio.h> // Including header file for printf function
#include <math.h> // Including header file for erf function
int main (){
float param, result;
param = 4.0;
result = erff(param);
printf("erff (%f) = %f\n", param, result);
return 0;
}

The following code snippet shows how error handling in erff function works:

#include <stdio.h> // Including header file for printf function
#include <math.h> // Including header file for erf function
int main (){
printf("erff (%f) = %f\n", 0.0, erff(0.0));
return 0;
}
Copyright ©2024 Educative, Inc. All rights reserved