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


C++ Math lgamma()用法及代碼示例

lgamma() 函數計算傳遞給函數的參數的伽馬函數的對數。

假設一個數字是 x:

C++ Math lgamma() function

用法

float lgamma(float x);
double lgamma(double x);
long double lgamma(long double x);
double lgamma(integral x);

參數

x: 它是一個浮點值。

返回值

它返回值 x 的伽馬函數的對數。

參數 返回值
x= 1 或 x=2 0
x=±0 +∞
x= -ve 整數或 ±∞ +∞
x= nan nan

例子1

讓我們看看 x 的值為 2 時的簡單示例。

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     int x=2;
     cout<<"Value of x is:"<<x<<'\n';
     cout<<"lgamma(x):"<<lgamma(x);
     return 0;
}

輸出:

Value of x is:2
lgamma(x):0

在上麵的例子中,x 的值為 2。因此,函數 lgamma() 返回 0 值。

例子2

讓我們看看 x 的值為 0 時的簡單示例。

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     int x=0;
     cout<<"Value of x is:"<<x<<'\n';
     cout<<"lgamma(x):"<<lgamma(x);
     return 0;
}

輸出:

Value of x is:0
lgamma(x):inf

在上麵的例子中,x 的值為零。因此,函數 lgamma() 返回 +∞。

例子3

讓我們看一個簡單的例子,當 x 的值是一個負整數時。

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     int x= -5;
     cout<<"Value of x is:"<<x<<'\n';
     cout<<"lgamma(x):"<<lgamma(x);
     return 0;
}

輸出:

Value of x is:-5
lgamma(x):inf

在上麵的例子中,x 的值是一個負整數。因此,函數 lgamma() 返回 +∞。

示例 4

讓我們看一個簡單的例子,當 x 的值為 nan 時。

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
     float x=sqrt(-6);
     cout<<"Value of x is:"<<x<<'\n';
     cout<<"lgamma(x):"<<lgamma(x);
     return 0;
}

輸出:

Value of x is:-nan
lgamma(x):-nan

在上麵的例子中,x 的值為 nan。因此,函數 lgamma() 返回 nan。






相關用法


注:本文由純淨天空篩選整理自 C++ Math lgamma()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。