名稱空間
變體
操作

tgamma, tgammaf, tgammal

來自 cppreference.com
< c‎ | 數值‎ | 數學
 
 
 
常用數學函式
函式
基本操作
(C99)
(C99)
(C99)
(C99)(C99)(C99)(C23)
最大值/最小值操作
(C99)
(C99)
指數函式
(C23)
(C99)
(C99)
(C23)
(C23)

(C99)
(C99)(C23)
(C23)
(C23)
冪函式
(C99)
(C23)
(C23)

(C99)
(C23)
(C23)
三角函式和雙曲函式
(C23)
(C23)
(C23)
(C23)
(C99)
(C99)
(C99)
最近整數浮點數
(C99)(C99)(C99)
(C99)

(C99)(C99)(C99)
(C23)(C23)(C23)(C23)
浮點數操作
(C99)(C99)
(C99)(C23)
(C99)
窄化操作
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
量子與量子指數
十進位制重新編碼函式
總序和載荷函式
分類
(C99)
(C99)
(C99)
(C23)
誤差函式和伽馬函式
(C99)
(C99)
(C99)
tgamma
(C99)
型別
宏常量
特殊浮點值
(C99)(C23)
引數和返回值
錯誤處理
快速操作指示符
 
定義於標頭檔案 <math.h>
float       tgammaf( float arg );
(1) (C99 起)
double      tgamma( double arg );
(2) (C99 起)
long double tgammal( long double arg );
(3) (C99 起)
定義於標頭檔案 <tgmath.h>
#define tgamma( arg )
(4) (C99 起)
1-3) 計算 arg伽馬函式
4) 型別泛型宏:若 arg 的型別是 long double,則呼叫 tgammal。否則,若 arg 的型別是整數型別或 double,則呼叫 tgamma。否則,呼叫 tgammaf

目錄

[編輯] 引數

arg - 浮點值

[編輯] 返回值

若無錯誤發生,返回 arg 的伽馬函式值,即
0
targ-1
e-t dt

若發生域錯誤,返回實現定義值(若支援,則為 NaN)。

若發生極點錯誤,返回 ±HUGE_VAL±HUGE_VALF±HUGE_VALL

若發生因溢位導致的範圍錯誤,則返回 ±HUGE_VAL±HUGE_VALF±HUGE_VALL

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

[編輯] 錯誤處理

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

arg 為零或小於零的整數,可能發生極點錯誤或域錯誤。

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

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

[編輯] 注意

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

對於 IEEE 相容的 double 型別,如果 0 < x < 1/DBL_MAXx > 171.7,則發生溢位。

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

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

[編輯] 示例

#include <errno.h>
#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
 
int main(void)
{
    printf("tgamma(10) = %f, 9!=%f\n", tgamma(10), 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9.0);
    printf("tgamma(0.5) = %f, sqrt(pi) = %f\n", tgamma(0.5), sqrt(acos(-1)));
 
    // special values
    printf("tgamma(+Inf) = %f\n", tgamma(INFINITY));
 
    // error handling
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("tgamma(-1) = %f\n", tgamma(-1));
    if (errno == ERANGE)
        perror("    errno == ERANGE");
    else
        if (errno == EDOM)   perror("    errno == EDOM");
    if (fetestexcept(FE_DIVBYZERO))
        puts("    FE_DIVBYZERO raised");
    else if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID raised");
}

可能的輸出

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

[編輯] 引用

  • C23 標準 (ISO/IEC 9899:2024)
  • 7.12.8.4 tgamma 函式 (p: 250)
  • 7.25 型別通用數學 <tgmath.h> (p: 373-375)
  • F.10.5.4 tgamma 函式 (p: 525)
  • C17 標準 (ISO/IEC 9899:2018)
  • 7.12.8.4 tgamma 函式 (p: 250)
  • 7.25 型別通用數學 <tgmath.h> (p: 373-375)
  • F.10.5.4 tgamma 函式 (p: 525)
  • C11 標準 (ISO/IEC 9899:2011)
  • 7.12.8.4 tgamma 函式 (p: 250)
  • 7.25 型別通用數學 <tgmath.h> (p: 373-375)
  • F.10.5.4 tgamma 函式 (p: 525)
  • C99 標準 (ISO/IEC 9899:1999)
  • 7.12.8.4 tgamma 函式 (p: 231)
  • 7.22 型別通用數學 <tgmath.h> (p: 335-337)
  • F.9.5.4 tgamma 函式 (p: 462)

[編輯] 亦參見

(C99)(C99)(C99)
計算伽馬函式的自然(底數e)對數
(function) [編輯]
C++ 文件 for tgamma

[編輯] 外部連結

Weisstein, Eric W. "Gamma Function." From MathWorld — A Wolfram Web Resource.