Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 计算负实值的伽马函数(C+;+;,Boost)_C++_Boost_C++17_Factorial_Gamma Function - Fatal编程技术网

C++ 计算负实值的伽马函数(C+;+;,Boost)

C++ 计算负实值的伽马函数(C+;+;,Boost),c++,boost,c++17,factorial,gamma-function,C++,Boost,C++17,Factorial,Gamma Function,以下公式要求计算非整数、负数和实数的阶乘: (这是一种计算椭圆周长的方法,a和b是半长轴和半短轴,h定义为: h=(a-b)^2/(a+b)^2) 阶乘函数可以通过伽玛函数扩展为负值,伽玛函数是为所有非负整数的实数定义的 在编写严肃的代码时,我尝试了boost::math::factorial和boost::math::tgamma,它们给出的结果只有-1(不包括)-1.5,例如,给出一个错误 #包括 #包括 int main() { 双x; 双f; 双甘油三酯; x=-0.5; f=boos

以下公式要求计算非整数、负数和实数的阶乘:

(这是一种计算椭圆周长的方法,a和b是半长轴和半短轴,h定义为:
h=(a-b)^2/(a+b)^2)

阶乘函数可以通过伽玛函数扩展为负值,伽玛函数是为所有非负整数的实数定义的

在编写严肃的代码时,我尝试了boost::math::factorial和boost::math::tgamma,它们给出的结果只有-1(不包括)-1.5,例如,给出一个错误

#包括
#包括
int main()
{
双x;
双f;
双甘油三酯;
x=-0.5;
f=boost::math::factorial(x);
tg=boost::math::tgamma(x);

cout我刚刚尝试在Mac上使用g++-10,使用boost.math提交哈希87929356790ad0(当前
develope
),重现您的问题,但我得到:

factorial of -0.5 = 1
tgamma of -0.5 = -3.54491

factorial of -1.5 = 1
tgamma of -1.5 = 2.36327

因此需要更多信息,如Boost版本、操作系统、编译器等。

自C++11[1]以来,Gamma函数都是标准库的一部分

用法如下:

#include <cmath>

std::tgamma(-0.5) # -3.5449077
std::lgamma(-0.5) # 1.2655121
#包括
标准:tgamma(-0.5)#-3.5449077
标准:lgamma(-0.5)#1.2655121
对于
long double
float
类型,您也可以相应地使用
tgamal
tgamaf


  • 我知道出了什么问题。
    boost::math::factorial
    函数根据定义接受一个
    无符号的
    整数

    template <class T>
    inline T factorial(unsigned i)
    {
       return factorial<T>(i, policies::policy<>());
    }
    
    最终会这样做:

    #0  boost::math::tgamma<long double, boost::math::policies::policy<boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy> > (a=4294967295, z=...)
        at /home/sehe/custom/boost_1_73_0/boost/math/special_functions/gamma.hpp:1994
    No locals.
    #1  0x0000555555558eb3 in boost::math::factorial<long double, boost::math::policies::policy<boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy, boost::math::policies::default_policy> > (i=4294967294, pol=...)
        at /home/sehe/custom/boost_1_73_0/boost/math/special_functions/factorials.hpp:44
            result = -0.667762310955655363645
    #2  0x0000555555558674 in boost::math::factorial<long double> (i=4294967294)
        at /home/sehe/custom/boost_1_73_0/boost/math/special_functions/factorials.hpp:53
    No locals.
    #3  0x0000555555557792 in foo (x=-2) at /home/sehe/Projects/stackoverflow/test.cpp:7
            f = <invalid float value>
    #4  0x000055555555791f in main () at /home/sehe/Projects/stackoverflow/test.cpp:16
    No locals.
    
    印刷品:

    tgamma    of 1 = 1
    
    tgamma    of 2 = 1
    
    tgamma    of 3 = 2
    
    tgamma    of 4 = 6
    
    tgamma    of 5 = 24
    
    tgamma    of -0.2 = -5.82115
    
    error at -2: "Error in function boost::math::tgamma<long double>(long double): Evaluation of tgamma at a negative integer -2."
    tgamma    of -0.5 = -3.54491
    
    tgamma    of -1.5 = 2.36327
    
    tgamma为1=1
    2的tgamma=1
    tgamma为3=2
    tgamma为4=6
    tgamma为5=24
    tgamma为-0.2=-5.82115
    在-2处出错:“函数boost::math::tgamma(长双精度)中出错:在负整数-2处对tgamma求值。”
    tgamma为-0.5=-3.54491
    tgamma为-1.5=2.36327
    

    它在
    -2
    处溢出是可以理解的,但这是正确的。

    出于好奇,为什么不使用
    std::tgamma
    ?在上一篇boost-tgamma文档中,提供了一个数字,表明在计算x=-1.5时应该没有问题。@Damien谢谢,我不知道std::tgamma存在。是的,boost文档中的图像显示boost::tgamma函数确实是为负域定义的,但域没有明确定义。也许您也可以提供您的平台详细信息?@user14717非常感谢,至少现在我知道,一般来说,它应该可以工作,即使不是在我的版本中,我的平台details:Ubuntu18.04 gcc版本9.3.0(Ubuntu9.3.0-11ubuntu0~18.04.1)libboost开发版本1.65.1.0ubuntu1实际上,是和否。我只是写了一个答案:0非常感谢。我尝试了std::tgamma以及您的代码和两个作品。非常感谢。
    #include <boost/math/special_functions/factorials.hpp>
    #include <iostream>
    
    void foo(long double x) {
        using namespace boost::math;
        try {
            auto tg = tgamma<long double>(x);
            std::cout << "tgamma    of " << x << " = " << tg << "\n" << std::endl;
        } catch(std::exception const& e) {
            std::cout << "error at " << x << ": " << std::quoted(e.what()) << std::endl;
        }
    }
    
    int main() {
        for (auto x : { 1., 2., 3., 4., 5., -.2, -2., -.5, -1.5 })
            foo(x);
    }
    
    tgamma    of 1 = 1
    
    tgamma    of 2 = 1
    
    tgamma    of 3 = 2
    
    tgamma    of 4 = 6
    
    tgamma    of 5 = 24
    
    tgamma    of -0.2 = -5.82115
    
    error at -2: "Error in function boost::math::tgamma<long double>(long double): Evaluation of tgamma at a negative integer -2."
    tgamma    of -0.5 = -3.54491
    
    tgamma    of -1.5 = 2.36327