Error functions using cmath in C++


We are given the variable and the task is to find the probability of the variable using an error function available in C++ STL. This function is available in the cmath header file in C++.

What is an Error function?

Error function in mathematics is also known as Gauss error function which is denoted by erf(). It is a special function that is used in probability, statistic and partial differential equations for the calculation of error that can occur. It is defined as −

There are two closely related error function −

  • complementary error function − It is defined as erfc x = 1 - erf x
  • imaginary error function − It is defined aserfi x = -ierf(ix), where i is the imaginary unit

Example

Input

i = 2.25

Output

erf(i) i.e. 0.998537

Input

i = 1.25

Output

erf(i) i.e. 0.9229

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
double probab(double i){
   double probab_1 = erf(i);
   return probab_1;
}
int main (){
   double i = 2.25;
   cout<<"Probability is :"<<probab(i)<< endl;
   return 0;
}

Output

Probability is : 0.998537

Example

 Live Demo

#include <iostream>
#include <cmath>
using namespace std;
double probab(double i){
   double probab_1 = erf(i);
   return probab_1;
}
int main (){
   double i = 1.25;
   cout<<"Probability is :"<<probab(i)<< endl;
   return 0;
}

Output

Probability is : 0.9229

Updated on: 17-Apr-2020

197 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements