Namespaces
Variants

tgamma, tgammaf, tgammal

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
tgamma
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <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
t arg-1
e -t d t

如果发生定义域错误,则返回一个由实现定义的值(在支持 NaN 的情况下返回 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)));
    // 特殊值
    printf("tgamma(+Inf) = %f\n", tgamma(INFINITY));
    // 错误处理
    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 函数 (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函数(页码:231)
  • 7.22 泛型数学 <tgmath.h>(页码:335-337)
  • F.9.5.4 tgamma函数(页码:462)

参见

(C99) (C99) (C99)
计算伽玛函数的自然(以 e 为底)对数
(函数)
C++ 文档 关于 tgamma

外部链接

魏斯坦,埃里克·W。《伽玛函数》。 摘自 MathWorld —— 一个 Wolfram 网络资源。