Namespaces
Variants

std:: lgamma, std:: lgammaf, std:: lgammal

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
定义于头文件 <cmath>
(1)
float lgamma ( float num ) ;

double lgamma ( double num ) ;

long double lgamma ( long double num ) ;
(C++23 前)
/*floating-point-type*/
lgamma ( /*floating-point-type*/ num ) ;
(C++23 起)
(C++26 起为 constexpr)
float lgammaf ( float num ) ;
(2) (C++11 起)
(C++26 起为 constexpr)
long double lgammal ( long double num ) ;
(3) (C++11 起)
(C++26 起为 constexpr)
SIMD 重载 (C++26 起)
定义于头文件 <simd>
template < /*math-floating-point*/ V >

constexpr /*deduced-simd-t*/ < V >

lgamma ( const V & v_num ) ;
(S) (C++26 起)
额外重载 (C++11 起)
定义于头文件 <cmath>
template < class Integer >
double lgamma ( Integer num ) ;
(A) (C++26 起为 constexpr)
1-3) 计算 num 伽玛函数 绝对值的自然对数。 库为所有 cv 未限定浮点类型提供了 std::lgamma 的重载版本作为参数类型。 (C++23 起)
S) SIMD 重载对 v_num 执行逐元素的 std::lgamma 运算。
(参见 math-floating-point deduced-simd-t 了解其定义。)
(since C++26)
A) 为所有整数类型提供了额外的重载,这些类型将被视为 double
(since C++11)

目录

参数

num - 浮点数或整数值

返回值

若无错误发生,则返回伽马函数对 num 取对数的值,即 log e |
0
t num-1
e -t d t |

如果发生极点错误,则返回 +HUGE_VAL +HUGE_VALF +HUGE_VALL

如果发生因溢出导致的范围错误,将返回 ±HUGE_VAL ±HUGE_VALF ±HUGE_VALL

错误处理

错误报告方式遵循 math_errhandling 中的规范。

如果 num 为零或小于零的整数,可能出现极点错误。

如果实现支持 IEEE 浮点算术 (IEC 60559),

  • 若参数为1,返回+0。
  • 若参数为2,返回+0。
  • 若参数为±0,返回+∞并引发 FE_DIVBYZERO
  • 若参数为负整数,返回+∞并引发 FE_DIVBYZERO
  • 若参数为±∞,返回+∞。
  • 若参数为NaN,返回NaN。

注释

如果 num 是自然数, std :: lgamma ( num ) 表示 num - 1 的阶乘的对数值。

POSIX 版本的 lgamma 不是线程安全的:每次函数执行都会将 num 的伽玛函数符号存储在静态外部变量 signgam 中。某些实现提供了 lgamma_r ,该函数将指向用户提供的 signgam 存储空间的指针作为第二个参数,因此是线程安全的。

存在一个名为 gamma 的非标准函数,但其定义并不一致。例如,glibc和4.2BSD版本的 gamma 执行的是 lgamma ,而4.4BSD版本的 gamma 执行的则是 tgamma

额外的重载并不要求完全按照 (A) 提供。只需确保对于整数类型的实参 num std :: lgamma ( num ) std :: lgamma ( static_cast < double > ( num ) ) 具有相同效果即可。

示例

#include <cerrno>
#include <cfenv>
#include <cmath>
#include <cstring>
#include <iostream>
// #pragma STDC FENV_ACCESS ON
const double pi = std::acos(-1); // 或 C++20 起的 std::numbers::pi
int main()
{
    std::cout << "lgamma(10) = " << std::lgamma(10)
              << ", log(9!) = " << std::log(std::tgamma(10))
              << ", exp(lgamma(10)) = " << std::exp(std::lgamma(10)) << '\n'
              << "lgamma(0.5) = " << std::lgamma(0.5)
              << ", log(sqrt(pi)) = " << std::log(std::sqrt(pi)) << '\n';
    // 特殊值
    std::cout << "lgamma(1) = " << std::lgamma(1) << '\n'
              << "lgamma(+Inf) = " << std::lgamma(INFINITY) << '\n';
    // 错误处理
    errno = 0;
    std::feclearexcept(FE_ALL_EXCEPT);
    std::cout << "lgamma(0) = " << std::lgamma(0) << '\n';
    if (errno == ERANGE)
        std::cout << "    errno == ERANGE: " << std::strerror(errno) << '\n';
    if (std::fetestexcept(FE_DIVBYZERO))
        std::cout << "    FE_DIVBYZERO raised\n";
}

输出:

lgamma(10) = 12.8018, log(9!) = 12.8018, exp(lgamma(10)) = 362880
lgamma(0.5) = 0.572365, log(sqrt(pi)) = 0.572365
lgamma(1) = 0
lgamma(+Inf) = inf
lgamma(0) = inf
    errno == ERANGE: 数值结果超出范围
    FE_DIVBYZERO raised

参见

(C++11) (C++11) (C++11)
伽玛函数
(函数)
C 文档 关于 lgamma

外部链接

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