如何使用提升多精度和提升数学来执行幂?

How to perform exponentiation using boost multiprecision and boost math?

提问人:bad_chemist 提问时间:8/21/2022 更新时间:8/21/2022 访问量:64

问:

我正在运行一个有偏差的蒙特卡罗模拟,我需要处理报告的能量。我基本上必须计算,其中能量是负数,贝塔可以在 100 左右。如果 ,开始输出 。 这是一个测试运行:exp(-beta*energy)energy = 90std::expinf

#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

C++ 提高 浮动精度

评论

0赞 bad_chemist 8/21/2022
感谢您@user1095108的回答。你是什么意思呢?你对我必须如何进行有什么建议吗?
1赞 n. m. could be an AI 8/21/2022
也许试试.boost::math::expm1<boost::multiprecision::cpp_dec_float_50>(900)

答:

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.0doublexcpp_dec_float_50

ADL 在关联的命名空间中查找正确的重载。exp