【发布时间】:2020-07-23 14:10:00
【问题描述】:
(这是一种计算椭圆周长的方法,a和b是半长轴和半短轴,h定义为:
h = (a-b)^2/(a+b)^2)
阶乘函数可以通过 Gamma 函数扩展为负值,该函数为所有非负整数的实数定义。
在编写严重代码时,我尝试了 boost::math::factorial 和 boost::math::tgamma,它只给出了 -1(不包括)-1.5 的结果,例如给出了一个错误。
#include <iostream>
#include <boost/math/special_functions/factorials.hpp>
int main()
{
double x;
double f;
double tg;
x = -0.5;
f = boost::math::factorial<double>(x);
tg = boost::math::tgamma<double>(x);
cout << "factorial of " << x << " = " << f << endl;
cout << "tgamma of " << x << " = " << tg << endl << endl;
x = -1.5;
f = boost::math::factorial<double>(x);
tg = boost::math::tgamma<double>(x);
cout << "factorial of " << x << " = " << f << endl;
cout << "tgamma of " << x << " = " << tg << endl << endl;
return 0;
}
输出:
-0.5 = 1的阶乘
-0.5 的 tgamma = -3.54491
在抛出 'boost::exception_detail::clone_implboost::exception_detail::error_info_injector<:domain_error>' 的实例后调用终止 what():函数 boost::math::tgamma(long double) 中的错误:在负整数 0 处评估 tgamma。 中止(核心转储)
提升阶乘:
boost factorial
提升 tgamma:
boost tgamma
我的问题:
- 是否有替代 boost 的方法可以计算其负域的 gamma 函数?
- 我在上面提到的阶乘和 tgamma 函数的实现域的链接的 boost 文档中找不到。事实上,我可能只是错误地使用它们。确定什么是域/正确用法的方法是什么?
谢谢。
【问题讨论】:
-
只是为了好奇,你为什么不使用
std::tgamma? en.cppreference.com/w/cpp/numeric/math/tgamma -
在上一个 boost tgamma 文档中,提供了一个数字,表明您在计算 x = -1.5 时应该没有问题。 boost.org/doc/libs/1_73_0/libs/math/doc/html/math_toolkit/…
-
@Damien 谢谢,我不知道 std::tgamma 存在。是的,来自 boost 文档的图像应用确实为负域定义了 boost::tgamma 函数,但没有明确定义域。
标签: c++ boost c++17 factorial gamma-function