C++ Math erf
The erf function calculates the value of the error function for a given parameter.
Syntax
Suppose a number is 'x':
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.
#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:
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.
#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:
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.
#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:
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.
#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:
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.