beta(), betaf() and betal() functions in C++ STL

Hello everyone, in this tutorial, we will learn about beta(), betaf() and betal() function in C++ STL. All these functions are defined in <cmath> header. These are built-in C++ functions that are used to calculate the value of the beta function of two given positive real values. To know about the beta function, see this post: Beta function.

The syntax for the beta(), betaf() and betal() functions in C++ is as follows:

double beta(double a, double b)

float beta(float a, float b)

long double(long double a, long double b)

As you can see the functions beta(), betaf() and betal() take two parameters as input and calculate the value of the beta function for them. The beta() function accepts input of type double, betaf() of type float and betal() accepts input of type long double. The return type for these functions is the same as the input type.

See the below C++ program for a better understanding of the beta functions.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double a1 = 3, b1 = 6;
    cout << "beta function of " << a1 << " and " << b1 << " is " << beta(a1, b1) << endl;
    
    float a2 = 3.5, b2 = 6.5;
    cout << "beta function of " << a2 << " and " << b2 << " is " << betaf(a2, b2) << endl;    
    
    long double a3 = 3.7, b3 = 6.8;
    cout << "beta function of " << a3 << " and " << b3 << " is " << betal(a3, b3) << endl;
    
    return 0;
}

The above program can execute only in C++17 or above.

Output:

beta function of 3 and 6 is 0.00595238
beta function of 3.5 and 6.5 is 0.00263653
beta function of 3.7 and 6.8 is 0.00182759

We can use the above beta functions for many purposes. One such application is calculating the binomial coefficient. Have a look at this code.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    double a = 5, b = 2;
   
    double bin_coeff = 1 / ((a + 1) * beta(a - b + 1, b + 1));
   
    cout << bin_coeff;
    
    return 0;
}

Output:

10

The above program calculates the binomial coefficient for the given input a and b.

You may also read: C++ program to convert infix to postfix

Leave a Reply

Your email address will not be published. Required fields are marked *