tgamma, tgammaf, tgammal
来自 cppreference.cn
定义于头文件 <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 起) |
4) 类型泛型宏:若 arg 的类型是 long double,则调用
tgammal
。否则,若 arg 的类型是整数类型或 double,则调用 tgamma
。否则,调用 tgammaf
。目录 |
[编辑] 参数
arg | - | 浮点值 |
[编辑] 返回值
若无错误发生,返回 arg 的伽马函数值,即 ∫∞
0targ-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 函数 (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. |