发布时间:2025-12-09 11:55:27 浏览次数:2
beta(),betaf()和betal()是C++ STL中的内置函数,用于计算两个正实数值的beta函数。该函数将两个变量x和y作为输入,并返回x和y的beta函数。 x和y的Beta函数(也称为第一类Euler积分)可以定义为:
用法
double beta(double x, double y)or long double betal(long double x, long double y)or float betaf(float x, float y)
参数:该函数接受两个强制性参数x和y,它们指定浮点或整数类型的值。参数可以是double,double或float,float或long double,long double数据类型。
返回值:该函数返回x和y的beta函数的值。返回类型取决于传递的参数。与参数相同。
注意:该函数在C++ 17(7.1)及更高版本中运行。
以下示例程序旨在说明beta(),betaf()和betal()函数:
// C++ program to illustrate the three functions // Being a special function, beta is only guaranteed // to be in cmath if the user defines // __STDCPP_WANT_MATH_SPEC_FUNCS__ before including // any standard library headers. #define __STDCPP_WANT_MATH_SPEC_FUNCS__ 1 #include <bits/stdc++.h> using namespace std; int main() { // Computes the beta function of 5 and 4 and print it // If provided arguments are not of type double // they are implicitly type-casted to the higher type. // first example of beta() cout << beta(5, 4) << "\n"; // second example of betaf() cout << betaf(10.0, 4.0) << "\n"; // third example of betal() cout << betal(10.0, 6.7) << "\n"; return 0; }输出:
0.003571430.000349651.65804e-005
Beta函数的应用:用于计算二项式系数。以beta函数表示的二项式系数可以表示为:
// C++ program to print the pascal triangle // Being a special function, beta is only guaranteed // to be in cmath if the user defines // __STDCPP_WANT_MATH_SPEC_FUNCS__ before including // any standard library headers. #define __STDCPP_WANT_MATH_SPEC_FUNCS__ 1 #include <bits/stdc++.h> #include <cmath> using namespace std; // Function to return the value of binomial Coefficient double binomialCoefficient(int n, int k) { // Calculate the value of nCr using above formula. double ans = 1 / ((n + 1) * beta(n - k + 1, k + 1)); return ans; } // Driver Code int main() { // Print the binomial Coefficient nCr where n=5 and r=2 cout << binomialCoefficient(5, 2) << "\n"; return 0; }输出:
10