gamma(double x)的定义是什么?为什么它在两个gcc版本上有所不同? [英] What is the definition for gamma(double x) and why is it different on two gcc versions?

查看:285
本文介绍了gamma(double x)的定义是什么?为什么它在两个gcc版本上有所不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不幸的情况下,我发现我的标准库实现< math.h> < cmath> (C ++)显然包含一个函数的定义,如下所示:

  double gamma(double x); 

虽然我没有看到它在语言标准(我有权访问的草稿)中的任何地方列出。



在Mac OS X上使用gcc v4.2.1,该函数的计算结果与 tgamma 相同,它实际上是给定的名称它在标准中。 (参考)



但是在Ubuntu 12.04上的gcc v4.6.3上,函数的计算结果与其他不同。



我不明白为什么名为 gamma 编译,但为什么编译器之间的结果不一样?



这是一个简单的测试程序:

 #include< iostream> 
#include< sstream>
#include< math.h> //或< cmath>

int main(int argc,char ** argv)
{
std :: stringstream conversionStream(argv [1]);
双输入;
conversionStream>>输入;
std :: cout<< gamma(<< input<)=<<伽马(输入)<<的std :: ENDL;
返回0;
}

编译并运行1个参数:

  $ g ++ -o gamma_test gamma_test.cpp 
$ ./gamma_test 1.0
gamma(1)= 1

但是Ubuntu gcc v4.6.3的输出是0!

解决方案

摘要:历史混乱盛行;避免 gamma()并使用 tgamma()



它是数学库,不是gcc(编译器),它实现了这些功能。如果您在MacOS和Ubuntu上看到不同的行为,可能是因为Ubuntu使用glibc和MacOS使用了其他功能。



没有名为的函数,伽马在ISO C标准库中。



标准函数称为 lgamma tgamma 。引用 N1570 (最新的草案2011年ISO C标准)第17.12.8.3和17.12.8.4节:


#include< math.h>

double lgamma(double x);

float lgammaf( float x);

long double lgammal(long double x);



lgamma 函数计算 x 的绝对
伽马值的自然对数。如果 x 太大,则会发生范围错误。如果 x 是一个负整数或零,则可能会出现一个极点
的错误。

#include< math.h>

double tgamma(double x);

float tgammaf(float x);

long double tgammal(long double x);



tgamma 函数计算 x 的伽马函数。如果 x 是负整数或零,则可能出现域错误
或极点错误。如果 x 的大小过大,并且如果 x
幅度太小,则会发生范围
错误。


这些函数不会出现在1990 ISO C标准中。它们是由C99引入的。

引用 gamma 的Linux手册页:


不推荐使用这些函数:相反,使用 tgamma (3)或
lgamma (3)函数。



有关Gamma函数的定义,请参阅 tgamma (3)。




  • * BSD版本


    <4.4> 4.4中的libm BSD和某些版本的FreeBSD有一个 gamma ()函数,可以计算Gamma函数,正如人们所期望的那样。

  • glibc版本


    Glibc有一个 gamma 函数相当于 lgamma (3)并计算Gamma函数的自然对数。





和历史记录:


4.2BSD有一个 gamma (),它计算ln(| Gamma(| x |)|),留下符号
Gamma(| x |)在外部整数signgam。在4.3BSD中,名称
被更改为 lgamma (3),并且手册页承诺
$ b 未来的名字伽马将被修复,
用于Gamma函数

这确实发生在4.4BSD中, gamma ()计算Gamma
函数(对signgam没有影响)。然而,这太迟了,
,现在我们已经有了(3),即真正的伽马功能。

既然 gamma 不是一个标准的C函数,编译时使用 gcc -std = c99 -pedantic gcc -std = c11 -pedantic 应该至少会产生一个警告,试图调用它。



如果你想要自然对数,你应该使用 tgamma()(或者 lgamma())并避免使用 gamma()



C标准似乎没有说明Gamma函数是什么。 Linux tgamma()手册页(但如果你'试图使用它,你可能已经知道它是什么了):

lockquote
Gamma函数定义为

Gamma(x)=从0到无穷大t ^(x-1)的积分e ^ -t dt

每一个实数,除了非正整数。

对于非负整数m有一个

(m + 1)= m!

$ (x + 1)= x * Gamma(x)

$ b

更一般地说,对于所有的x:

此外,以下内容适用于
极点以外的所有x值:


Gamma(x) * Gamma(1-x)= PI / sin(PI * x)


Through unfortunate circumstances I have discovered that my standard library implementations <math.h> and <cmath>(C++) apparently contain a definition for a function with a prototype like:

double gamma(double x);

Though I do not see it listed anywhere in the language standard (draft I have access to).

Using gcc v4.2.1 on Mac OS X that function evaluates the same as tgamma which is actually the name given to it in the standard. (reference)

But on gcc v4.6.3 on Ubuntu 12.04 that function evaluates to something different.

I don't understand why a function named gamma compiles at all, but why is the result not the same between compilers?

Here is a simple test program:

#include<iostream>
#include<sstream>
#include<math.h> // or <cmath>

int main(int argc, char **argv)
{
    std::stringstream conversionStream(argv[1]);
    double input;
    conversionStream >> input;
    std::cout << "gamma( " << input << " ) = " << gamma(input) << std::endl;
    return 0;
}

Compile and run with 1 argument:

$ g++ -o gamma_test gamma_test.cpp 
$ ./gamma_test 1.0
gamma( 1 ) = 1

But the output on Ubuntu gcc v4.6.3 is 0!

解决方案

Summary: Historical confusion abounds; avoid gamma() and use tgamma().

It's the math library, not gcc (the compiler), that implements these functions. If you're seeing different behavior on MacOS and Ubuntu, it's probably because Ubuntu uses glibc and MacOS uses something else.

There is no function named gamma in the ISO C standard library.

There are standard functions called lgamma and tgamma. Quoting N1570 (the latest draft of the 2011 ISO C standard) sections 17.12.8.3 and 17.12.8.4:

#include <math.h>
double lgamma(double x);
float lgammaf(float x);
long double lgammal(long double x);

The lgamma functions compute the natural logarithm of the absolute value of gamma of x. A range error occurs if x is too large. A pole error may occur if x is a negative integer or zero.

#include <math.h>
double tgamma(double x);
float tgammaf(float x);
long double tgammal(long double x);

The tgamma functions compute the gamma function of x. A domain error or pole error may occur if x is a negative integer or zero. A range error occurs if the magnitude of x is too large and may occur if the magnitude of x is too small.

These functions do not appear in the 1990 ISO C standard. They were introduced by C99.

Quoting the Linux man page for gamma:

These functions are deprecated: instead, use either the tgamma(3) or the lgamma(3) functions, as appropriate.

For the definition of the Gamma function, see tgamma(3).

  • *BSD version

    The libm in 4.4BSD and some versions of FreeBSD had a gamma() function that computes the Gamma function, as one would expect.

  • glibc version

    Glibc has a gamma() function that is equivalent to lgamma(3) and computes the natural logarithm of the Gamma function.

and an historical note:

4.2BSD had a gamma() that computed ln(|Gamma(|x|)|), leaving the sign of Gamma(|x|) in the external integer signgam. In 4.3BSD the name was changed to lgamma(3), and the man page promises

"At some time in the future the name gamma will be rehabilitated and used for the Gamma function"

This did indeed happen in 4.4BSD, where gamma() computes the Gamma function (with no effect on signgam). However, this came too late, and we now have tgamma(3), the "true gamma" function.

Since gamma is not a standard C function, compiling with gcc -std=c99 -pedantic or gcc -std=c11 -pedantic should produce at least a warning for any attempt to call it.

You should probably use tgamma() (or lgamma() if you want the natural logarithm) and avoid using gamma().

The C standard doesn't seem to say what the Gamma function is. The Linux tgamma() man page does (but if you're trying to use it you probably already know what it is):

The Gamma function is defined by

Gamma(x) = integral from 0 to infinity of t^(x-1) e^-t dt

It is defined for every real number except for nonpositive integers.
For nonnegative integral m one has

Gamma(m+1) = m!

and, more generally, for all x:

Gamma(x+1) = x * Gamma(x)

Furthermore, the following is valid for all values of x outside the poles:

Gamma(x) * Gamma(1 - x) = PI / sin(PI * x)

这篇关于gamma(double x)的定义是什么?为什么它在两个gcc版本上有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆