std::tgamma, std::tgammaf, std::tgammal
在标头 <cmath> 定义 | ||
(1) | ||
float tgamma (float num ); double tgamma (double num ); | (C++23 前) | |
/* 浮点数类型 */ tgamma (/* 浮点数类型 */ num ); | (C++23 起) (C++26 起 constexpr) | |
float tgammaf(float num ); | (2) | (C++11 起) (C++26 起为 constexpr) |
longdouble tgammal(longdouble 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 。
| (C++26 起) |
A) 为所有整数类型提供额外重载,将它们当做 double。 | (C++11 起) |
目录 |
[编辑]参数
num | - | 浮点数或整数 |
[编辑]返回值
如果没有发生错误,那么返回 num 的 Γ 函数值,即 ∫∞
0tnum-1
e-t dt。
如果发生定义域错误,那么返回值由实现定义(在受支持平台上是 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
的非标准函数,但它们的定义不一致。例如,gamma
的 glibc 和 4.2BSD 版本执行 lgamma
,但 gamma
的 4.4BSD 版本执行 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\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: Numerical argument out of domain 发生 FE_INVALID
[编辑]参阅
(C++11)(C++11)(C++11) | gamma 函数的自然对数 (函数) |
(C++17)(C++17)(C++17) | beta 函数 (函数) |
tgamma 的 C 文档 |
[编辑]外部链接
Weisstein, Eric W. “伽马函数”来自 MathWorld--A Wolfram Web Resource。 |