當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ beta(), betaf() and betal()用法及代碼示例

beta(),betaf()和betal()是C++ STL中的內置函數,用於計算兩個正實數值的beta函數。該函數將兩個變量x和y作為輸入,並返回x和y的beta函數。 x和y的Beta函數(也稱為第一類Euler積分)可以定義為:

 $$B(x, y)=\int_0^1 t^{x-1} (1-t)^{y-1} dt $$

用法


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.00357143
0.00034965
1.65804e-005

Beta函數的應用:用於計算二項式係數。以beta函數表示的二項式係數可以表示為:

  $$\binom{n}{k}= \frac{1}{(n+1)B(n-k+1, k+1)}$$

以上關係可用於計算二項式係數。圖示如下:

// 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


相關用法


注:本文由純淨天空篩選整理自Pathak7大神的英文原創作品 beta(), betaf() and betal() functions in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。