std:: tgamma, std:: tgammaf, std:: tgammal
|
定义于头文件
<cmath>
|
||
| (1) | ||
|
float
tgamma
(
float
num
)
;
double
tgamma
(
double
num
)
;
|
(C++23 前) | |
|
/*floating-point-type*/
tgamma ( /*floating-point-type*/ num ) ; |
(C++23 起)
(C++26 起为 constexpr) |
|
|
float
tgammaf
(
float
num
)
;
|
(2) |
(C++11 起)
(C++26 起为 constexpr) |
|
long
double
tgammal
(
long
double
num
)
;
|
(3) |
(C++11 起)
(C++26 起为 constexpr) |
|
SIMD 重载
(C++26 起)
|
||
|
定义于头文件
<simd>
|
||
|
template
<
/*math-floating-point*/
V
>
constexpr
/*deduced-simd-t*/
<
V
>
|
(S) | (C++26 起) |
|
附加重载
(C++11 起)
|
||
|
定义于头文件
<cmath>
|
||
|
template
<
class
Integer
>
double tgamma ( Integer num ) ; |
(A) | (C++26 起为 constexpr) |
|
S)
SIMD 重载对
v_num
执行逐元素的
std::tgamma
运算。
|
(since C++26) |
|
A)
为所有整数类型提供了额外的重载,这些类型被视为
double
。
|
(since C++11) |
目录 |
参数
| num | - | 浮点数或整数值 |
返回值
若无错误发生,则返回
num
的伽玛函数值,即
∫
∞
0
t
num-1
e
-t
d
t
。
如果发生定义域错误,则返回一个由实现定义的值(在支持 NaN 的情况下返回 NaN)。
若出现极点错误,则返回
±HUGE_VAL
、
±HUGE_VALF
或
±HUGE_VALL
。
如果发生因溢出导致的范围错误,将返回
±HUGE_VAL
、
±HUGE_VALF
或
±HUGE_VALL
。
如果发生因下溢导致的范围错误,将返回正确值(四舍五入后)。
错误处理
错误报告方式遵循 math_errhandling 中的规范。
如果 num 为零或小于零的整数,可能出现极点错误或定义域错误。
如果实现支持 IEEE 浮点算术 (IEC 60559),
- 若参数为 ±0,则返回 ±∞ 并引发 FE_DIVBYZERO 。
- 若参数为负整数,则返回 NaN 并引发 FE_INVALID 。
- 若参数为 -∞,则返回 NaN 并引发 FE_INVALID 。
- 若参数为 +∞,则返回 +∞。
- 若参数为 NaN,则返回 NaN。
注释
如果 num 是自然数, std :: tgamma ( num ) 的值等于 num - 1 的阶乘。许多实现在参数为足够小的整数时,会计算精确的整数域阶乘值。
对于 IEEE 兼容类型 double ,当 0 < num && num < 1 / DBL_MAX 或 num > 171.7 时会发生溢出。
POSIX 标准要求 :当参数为零时发生极点错误,但当参数为负整数时发生定义域错误。同时规定未来版本中,针对负整数参数的定义域错误可能被极点错误取代(届时这些情况下的返回值将从 NaN 变更为 ±∞)。
存在一个名为
gamma
的非标准函数,但其定义并不一致。例如,glibc和4.2BSD版本的
gamma
执行的是
lgamma
,而4.4BSD版本的
gamma
执行的则是
tgamma
。
额外的重载并不要求必须完全按照 (A) 的形式提供。只需确保对于整数类型的实参 num , std :: tgamma ( num ) 与 std :: tgamma ( static_cast < double > ( num ) ) 具有相同效果即可。
示例
#include <cerrno> #include <cfenv> #include <cmath> #include <cstring> #include <iostream> // #pragma STDC FENV_ACCESS ON int main() { std::cout << "tgamma(10) = " << std::tgamma(10) << ", 9! = " << 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 << '\n' << "tgamma(0.5) = " << std::tgamma(0.5) << ", sqrt(pi) = " << std::sqrt(std::acos(-1)) << '\n'; // 特殊值 std::cout << "tgamma(1) = " << std::tgamma(1) << '\n' << "tgamma(+Inf) = " << std::tgamma(INFINITY) << '\n'; // 错误处理 errno = 0; std::feclearexcept(FE_ALL_EXCEPT); std::cout << "tgamma(-1) = " << std::tgamma(-1) << '\n'; if (errno == EDOM) std::cout << " errno == EDOM: " << std::strerror(errno) << '\n'; if (std::fetestexcept(FE_INVALID)) std::cout << " FE_INVALID raised\n"; }
可能的输出:
tgamma(10) = 362880, 9! = 362880
tgamma(0.5) = 1.77245, sqrt(pi) = 1.77245
tgamma(1) = 1
tgamma(+Inf) = inf
tgamma(-1) = nan
errno == EDOM: 数值参数超出定义域
FE_INVALID raised
参见
|
(C++11)
(C++11)
(C++11)
|
gamma 函数的自然对数
(函数) |
|
(C++17)
(C++17)
(C++17)
|
beta 函数
(函数) |
|
C 文档
for
tgamma
|
|
外部链接
| Weisstein, Eric W. "Gamma Function." 摘自 MathWorld —— 一个 Wolfram 网络资源。 |