提问人:bad_chemist 提问时间:8/21/2022 更新时间:8/21/2022 访问量:64
如何使用提升多精度和提升数学来执行幂?
How to perform exponentiation using boost multiprecision and boost math?
问:
我正在运行一个有偏差的蒙特卡罗模拟,我需要处理报告的能量。我基本上必须计算,其中能量是负数,贝塔可以在 100 左右。如果 ,开始输出 。
这是一个测试运行:exp(-beta*energy)
energy = 90
std::exp
inf
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/math/special_functions/expm1.hpp>
#include <iostream>
#include <cmath>
int main()
{
// using namespace boost::multiprecision;
double u = std::exp (900.0);
boost::multiprecision::cpp_dec_float_50 v = 2.71;
// loop for e^900
for(unsigned i = 1; i < 901; ++i){
v *= 2.71;
}
boost::multiprecision::cpp_dec_float_50 x = boost::math::expm1 (900.0);
std::cout << "u = " << u << std::endl;
std::cout << "v = " << v << std::endl;
std::cout << "x = " << x << std::endl;
return 0;
}
结果:
u = inf
v = 1.27447e+390
x = inf
我的问题是,我如何执行这个幂,并得到像 中一样的答案?v
答:
1赞
sehe
8/21/2022
#1
只需使用!Live On 编译器资源管理器exp
boost::multiprecision::cpp_dec_float_50 x = 900.0;
std::cout << "v = " << exp(x) << std::endl;
指纹
v = 7.32881e+390
和之间的区别在于,类型是 和 。exp(900.0)
exp(x)
900.0
double
x
cpp_dec_float_50
ADL 在关联的命名空间中查找正确的重载。exp
评论
boost::math::expm1<boost::multiprecision::cpp_dec_float_50>(900)