C erf() Function

Definition
In C, erf() returns the error function of x. It appears in probability and statistics with the normal distribution. erf(0) is 0.
You pass one double. The result is a double between -1 and 1. Include <math.h>. On GCC, link with -lm.
Syntax
Pass the argument. Start with this call.
| Call | What it does |
|---|
erf(0.0) | Returns 0. |
erf(0.5) | Returns a value near 0.52. |
Include <math.h>. On GCC, link with -lm. For large |x|, see also erfc().
Library declarations
double erf(double x);
float erff(float x);
long double erfl(long double x);
Parameters
| Parameter | Description |
|---|
| x | Required. The double argument to the error function. |
Return Value
| Return Value | Description |
|---|
| double | erf(x), a value in the closed range [-1, 1]. erf(0) is 0. |
C Examples
Example 1 — Basic Error Function
Print erf at a few simple points.
#include <stdio.h>
#include <math.h>
int main(void) {
printf("%.6f\n", erf(0.0));
printf("%.6f\n", erf(0.5));
printf("%.6f\n", erf(-0.5));
return 0;
}
0.000000
0.520500
-0.520500
How It Works
- Include math.h. That declares
erf. - Zero input.
erf(0) is exactly 0. - Positive input.
erf(0.5) is about 0.5205. - Odd function.
erf(-x) equals -erf(x).
Example 2 — Small Value Table
Loop over a few x values and print erf(x).
#include <stdio.h>
#include <math.h>
int main(void) {
double xs[] = { 0.0, 0.25, 0.50, 1.00, 2.00 };
int i;
for (i = 0; i < 5; i++) {
printf("erf(%.2f) = %.6f\n", xs[i], erf(xs[i]));
}
return 0;
}
erf(0.00) = 0.000000
erf(0.25) = 0.276326
erf(0.50) = 0.520500
erf(1.00) = 0.842701
erf(2.00) = 0.995322
How It Works
- Store sample points. An array holds a few x values.
- Call erf in a loop. Print each pair.
- Watch the growth. Values rise toward 1 as x grows.
- Use for checks. Handy when you verify library output.
Example 3 — Relate erf and erfc
erfc(x) is defined as 1 - erf(x). Compare both forms.
#include <stdio.h>
#include <math.h>
int main(void) {
double x = 0.75;
double a = 1.0 - erf(x);
double b = erfc(x);
printf("1 - erf = %.8f\n", a);
printf("erfc = %.8f\n", b);
return 0;
}
1 - erf = 0.28843573
erfc = 0.28843573
How It Works
- Pick a mid-range x. 0.75 keeps both forms accurate.
- Compute 1 - erf. Manual complement.
- Call erfc. Library complementary error function.
- Compare. For moderate x they match closely.
Frequently Asked Questions
erf() returns the error function of x. erf(0) is 0, and values stay between -1 and 1.
x is required. It is a double argument to the error function.
A double in [-1, 1]. Large positive x approaches 1.
Include <math.h>. On GCC, link with -lm.
Use erfc() when you need 1 - erf(x) for large x. Subtracting from 1 can lose precision.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful