如何使用boost multiprecision计算大int和double数

How to use boost multiprecision to calculate large int and double numbers

我想在大整数和双精度之间进行计算,例如, 1245.......889 * 3.14 我认为我们不能从 3.14 构造一个 cpp_int 因为 http://www.boost.org/doc/libs/1_56_0/libs/multiprecision/doc/html/boost_multiprecision/tut/conversions.html

另外我不确定我是否可以使用cpp_dec_float因为cpp_dec_float需要指定不能任意大的有效位数。

这是否意味着我应该使用 cpp_rational?但是我必须先将 3.14 转换为有理数,比如? how can I extract the mantissa of a double

我们有没有更好的方法来同时表示 3.14 和 large int 之类的双精度数?

谢谢,

你的问题似乎很混乱,但这里是:

您可以通过为精度指定 0 来使用具有动态精度的 gmp_float:

Live On Coliru

#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/gmp.hpp>
#include <iostream>

int main() {
    using Int = boost::multiprecision::cpp_int;
    using Float = boost::multiprecision::number<boost::multiprecision::gmp_float<0>>;

    Float fake_pi;
    boost::multiprecision::default_ops::calc_pi(fake_pi.backend(), 2000);

    Int value("12345678901234567890123456789012345678901234567890");

    std::cout << std::fixed << value << " * " << fake_pi << " = "  << Float(value.convert_to<Float>() * fake_pi);
}

版画

12345678901234567890123456789012345678901234567890 * 3.141593 = 38785094139697029053093797030280437291228399875653.959648