Math Erf Function - C++ Programming Tutorial
C++ Course / Math Functions / Math Erf Function

Math Erf Function

BLUF: Mastering Math Erf Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Math Erf Function

C++ is renowned for its efficiency. Learn how Math Erf Function enables low-level control and high-performance computing in the tutorial below.

C++ Math erf

The erf function calculates the value of the error function for a given parameter.

Syntax

Suppose a number is 'x':

Example

float erf( float x);
double erf( double x);
long double erf( long double x);
double erf( integral x);

Parameter

x : It is a floating point value.

Return value

It returns the error function value of x.

Parameter Return value
x=±0 ±0
x=± infinite ±1
x=nan nan

Example 1

Let's see the simple example.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     double x= 6.2;
     cout<<"Value of x is : "<<x<<'\n';
     cout<<"erf(x) : "<<erf(x);
     return 0;}

Output:

Output

Value of x is : 6.2
erf(x) : 1

In the previously mentioned case, the variable x is 6.2. The function erf provides the result, which is 1.

Example 2

Let's consider a basic scenario where the value of x approaches infinity.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     double x= 1.0/0.0;
     cout<<"Value of x is : "<<x<<'\n';
     cout<<"erf(x) : "<<erf(x);
     return 0;
}

Output:

Output

Value of x is : inf
erf(x) : 1

In the provided scenario, the variable x is considered to be unbounded. As a result, the function erf will yield a result of 1.

Example 3

Let's consider a basic scenario where the variable x is equal to zero.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     float x= 0.0;
     cout<<"Value of x is : "<<x<<'\n';
     cout<<"erf(x) : "<<erf(x);
     return 0;
}

Output:

Output

Value of x is : 0
erf(x) : 0

In the provided example, the variable x is set to zero. As a result, the function erf will yield a return value of 0.

Example 4

Let's explore a basic scenario where the value of x equals NaN.

Example

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     float x= 0.0/0.0;
     cout<<"Value of x is : "<<x<<'\n';
     cout<<"erf(x) : "<<erf(x);
     return 0;
}

Output:

Output

Value of x is : -nan
erf(x) : -nan

In the previous illustration, x is evaluated as NaN. Consequently, the erf function will yield NaN as the result.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience