命名空间
变体
操作

tgamma, tgammaf, tgammal

来自 cppreference.cn
< c‎ | numeric‎ | math
 
 
 
常用数学函数
函数
基本运算
(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_MAX 或若 x > 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 函数 (页: 250)
  • 7.25 类型泛型数学 <tgmath.h> (页: 373-375)
  • F.10.5.4 tgamma 函数 (页: 525)
  • C17 标准 (ISO/IEC 9899:2018)
  • 7.12.8.4 tgamma 函数 (页: 250)
  • 7.25 类型泛型数学 <tgmath.h> (页: 373-375)
  • F.10.5.4 tgamma 函数 (页: 525)
  • C11 标准 (ISO/IEC 9899:2011)
  • 7.12.8.4 tgamma 函数 (页: 250)
  • 7.25 类型泛型数学 <tgmath.h> (页: 373-375)
  • F.10.5.4 tgamma 函数 (页: 525)
  • C99 标准 (ISO/IEC 9899:1999)
  • 7.12.8.4 tgamma 函数 (页: 231)
  • 7.22 类型泛型数学 <tgmath.h> (页: 335-337)
  • F.9.5.4 tgamma 函数 (页: 462)

[编辑] 参见

(C99)(C99)(C99)
计算伽玛函数的自然对数(底 e
(函数) [编辑]
C++ 文档 关于 tgamma

[编辑] 外部链接

Weisstein, Eric W. "Gamma Function." 来自 MathWorld — Wolfram Web Resource。