名稱空間
變體
操作

std::tgamma, std::tgammaf, std::tgammal

來自 cppreference.com
< cpp‎ | 數值‎ | 數學
 
 
 
常用數學函式
函式
基本操作
(C++11)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
指數函式
(C++11)
(C++11)

(C++11)
(C++11)
冪函式
(C++11)
(C++11)
三角
雙曲函式
(C++11)
(C++11)
(C++11)

誤差函式和伽馬函式
(C++11)
(C++11)
(C++11)
tgamma
(C++11)
取整浮點運算
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
浮點操縱函式
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
分類和比較
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
型別
(C++11)
(C++11)
(C++11)
宏常量
分類
(C++11)(C++11)(C++11)(C++11)(C++11)


 
定義於標頭檔案 <cmath>
(1)
float       tgamma ( float num );

double      tgamma ( double num );

long double tgamma ( long double num );
(直至 C++23)
/*浮點型別*/
            tgamma ( /*浮點型別*/ num );
(C++23 起)
(C++26 起為 constexpr)
float       tgammaf( float num );
(2) (C++11 起)
(C++26 起為 constexpr)
long double tgammal( long double num );
(3) (C++11 起)
(C++26 起為 constexpr)
SIMD 過載 (C++26 起)
定義於標頭檔案 <simd>
template< /*數學浮點型別*/ V >

constexpr /*推導 SIMD 型別*/<V>

            tgamma ( const V& v_num );
(S) (C++26 起)
額外過載 (自 C++11 起)
定義於標頭檔案 <cmath>
template< class Integer >
double      tgamma ( Integer num );
(A) (C++26 起為 constexpr)
1-3) 計算 num伽馬函式 庫為所有 cv-不合格的浮點型別提供 std::tgamma 的過載作為引數的型別。(C++23 起)
S) SIMD 過載在 v_num 上執行元素級的 std::tgamma
(有關它們的定義,請參閱 math-floating-pointdeduced-simd-t。)
(C++26 起)
A) 為所有整數型別提供了額外的過載,它們被視為 double
(C++11 起)

目錄

[編輯] 引數

num - 浮點值或整數值

[編輯] 返回值

如果沒有錯誤發生,則返回 num 的伽馬函式值,即
0
tnum-1
e-t dt

如果發生域錯誤,則返回一個由實現定義的值(如果支援,則為 NaN)。

如果發生極點錯誤,則返回 ±HUGE_VAL±HUGE_VALF±HUGE_VALL

如果發生溢位導致的範圍錯誤,返回 ±HUGE_VAL±HUGE_VALF±HUGE_VALL

如果發生下溢導致的範圍錯誤,返回正確的值(舍入後)。

[編輯] 錯誤處理

錯誤按 math_errhandling 指定的方式報告。

如果 num 為零或小於零的整數,則可能發生極點錯誤或域錯誤。

如果實現支援 IEEE 浮點運算 (IEC 60559),

  • 如果引數為 ±0,則返回 ±∞ 並引發 FE_DIVBYZERO
  • 如果引數為負整數,則返回 NaN 並引發 FE_INVALID
  • 如果引數為 -∞,則返回 NaN 並引發 FE_INVALID
  • 如果引數是 +∞,則返回 +∞。
  • 如果引數為 NaN,則返回 NaN。

[編輯] 註記

如果 num 是自然數,則 std::tgamma(num)num - 1 的階乘。如果引數是足夠小的整數,許多實現會計算精確的整數域階乘。

對於 IEEE 相容型別 double,當 0 < num && num < 1 / DBL_MAX 或當 num > 171.7 時,會發生溢位。

POSIX 要求,如果引數為零,則發生極點錯誤,但如果引數為負整數,則發生域錯誤。它還規定,將來,對於負整數引數,域錯誤可能會被極點錯誤取代(在這種情況下,這些情況下的返回值將從 NaN 變為 ±∞)。

各種實現中存在一個名為 gamma 的非標準函式,但其定義不一致。例如,glibc 和 4.2BSD 版本的 gamma 執行 lgamma,但 4.4BSD 版本的 gamma 執行 tgamma

不需要完全按照 (A) 提供額外的過載。它們只需要足以確保對於整數型別的引數 numstd::tgamma(num) 具有與 std::tgamma(static_cast<double>(num)) 相同的效果。

[編輯] 示例

#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <iostream>
// #pragma STDC FENV_ACCESS ON
 
int main()
{
    std::cout << "tgamma(10) = " << std::tgamma(10)
              << ", 9! = " << 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 << '\n'
              << "tgamma(0.5) = " << std::tgamma(0.5)
              << ", sqrt(pi) = " << std::sqrt(std::acos(-1)) << '\n';
 
    // special values
    std::cout << "tgamma(1) = " << std::tgamma(1) << '\n'
              << "tgamma(+Inf) = " << std::tgamma(INFINITY) << '\n';
 
    // error handling
    errno = 0;
    std::feclearexcept(FE_ALL_EXCEPT);
 
    std::cout << "tgamma(-1) = " << std::tgamma(-1) << '\n';
 
    if (errno == EDOM)
        std::cout << "    errno == EDOM: " << std::strerror(errno) << '\n';
    if (std::fetestexcept(FE_INVALID))
        std::cout << "    FE_INVALID raised\n";
}

可能的輸出

tgamma(10) = 362880, 9! = 362880
tgamma(0.5) = 1.77245, sqrt(pi) = 1.77245
tgamma(1) = 1
tgamma(+Inf) = inf
tgamma(-1) = nan
    errno == EDOM: Numerical argument out of domain
    FE_INVALID raised

[編輯] 參閱

(C++11)(C++11)(C++11)
伽馬函式的自然對數
(函式) [編輯]
(C++17)(C++17)(C++17)
Beta 函式
(函式) [編輯]
C 文件 關於 tgamma

[編輯] 外部連結

Weisstein, Eric W. "伽馬函式。" 來自 MathWorld — Wolfram Web 資源。