erf | Error functions using cmath in C++

In mathematics, the error function (also called the Gauss error function) is a special function (non-elementary) of sigmoid shape that occurs in probability, statistics, and partial differential equations describing diffusion.

It’s the probability that a normal random variable with mean 0 and variance 0.5 take value between [-x, x].  It is denoted by erf(x) and calculated by:-

{\displaystyle {\begin{aligned}\operatorname {erf} (x)&={\frac {1}{\sqrt {\pi }}}\int _{-x}^{x}e^{-t^{2}}\,dt\\[5pt]&={\frac {2}{\sqrt {\pi }}}\int _{0}^{x}e^{-t^{2}}\,dt.\end{aligned}}}



In cmath library of C++, error function has been already implemented. There are two such functions:-

  1. erf(x) : This inbuilt function calculate the error function for input value of x. Parameter x which can be int or float or double. It return a double which is erf(x).
  2.  erfc(x) : This inbuilt function calculate the complementary of error function for input value of x. Parameter x which can be int or float or double. It return a double which is 1 – erf(x).
filter_none

edit
close

play_arrow

link
brightness_4
code

/* C++ code to use erf */
#include <iostream>
#include <cmath>     /* erf */
  
using namespace std;
  
double findProbability(double a)
{
    double prob_x_a = erf(a);
    return prob_x_a;
}
  
int main ()
{
    double a = 0.25;
    cout << "probability that normal r.v "
            "X takes value between " 
         << -a << " & " << a << " is " 
         << findProbability(a) << endl;
    return 0;
}

chevron_right


Output:

probability that normal r.v X takes value
between -0.25 & 0.25 is 0.276326

Reference :
Wikipedia

This article is contributed by Pratik Chhajer. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.