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 函数 (页: 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。 |