siv*_*ani 5 c++ boost factorial gamma-function c++17
(这是计算椭圆周长的一种方法,a和b是半长轴和半短轴,h定义为:
h = (ab)^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;
}
Run Code Online (Sandbox Code Playgroud)
输出:
-0.5 = 1
tgamma of -0.5 = -3.54491 的
阶乘在抛出 'boost::exception_detail::clone_implboost::exception_detail::error_info_injector<std::domain_error >' What() 实例后终止调用:函数 boost 中出错: :math::tgamma(long double):以负整数 0 计算 tgamma。中止(核心转储)
增强阶乘:
增强阶乘
增强 tgamma:
增强 tgamma
我的问题:
谢谢。
我明白出了什么问题。该函数根据定义boost::math::factorial接受一个unsigned整数:
template <class T>
inline T factorial(unsigned i)
{
return factorial<T>(i, policies::policy<>());
}
Run Code Online (Sandbox Code Playgroud)
这意味着如果您使用 double 调用它,它将隐式转换为无符号。那不是你想要的。另外,factorial最终tgamma在内部使用,所以你得到这个:
#include <boost/math/special_functions/factorials.hpp>
#include <iostream>
void foo(long double x) {
using namespace boost::math;
try {
auto f = factorial<long double>(x);
std::cout << "factorial of " << static_cast<unsigned>(x) << " = " << f << "\n";
} catch(std::exception const& e) {
std::cout << "error at " << static_cast<unsigned>(x) << ": " << std::quoted(e.what()) << "\n";
}
}
int main() {
std::cout << std::unitbuf;
foo(-2);
}
Run Code Online (Sandbox Code Playgroud)
最终会这样做:
#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.
Run Code Online (Sandbox Code Playgroud)
所以它试图给你boost::math::factorial<long double> (i=4294967294)
请勿用于factorials非负整数以外的其他整数。
#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);
}
Run Code Online (Sandbox Code Playgroud)
印刷:
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
Run Code Online (Sandbox Code Playgroud)
可以理解,它在 处溢出-2,但这是正确的。
| 归档时间: |
|
| 查看次数: |
700 次 |
| 最近记录: |