Joe C
Joe C

Reputation: 2827

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

I wanted to do calculation among large integers and double, for example, 1245.....889 * 3.14 I think we cannot construct a cpp_int from 3.14 because of http://www.boost.org/doc/libs/1_56_0/libs/multiprecision/doc/html/boost_multiprecision/tut/conversions.html

Also I am not sure if I can use cpp_dec_float because cpp_dec_float needs to specify the number of significant bits which cannot be arbitrarily large.

Does it mean I should use cpp_rational? But I have to convert 3.14 in a rational number first like? how can I extract the mantissa of a double

Do we have any better way to represent double like 3.14 and large int together?

Thank you,

Upvotes: 0

Views: 1731

Answers (1)

sehe
sehe

Reputation: 393567

Your question seems amply confused, but here goes:

You can use the gmp_float with dynamic precision by specifying 0 for the precision:

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);
}

Prints

12345678901234567890123456789012345678901234567890 * 3.141593 = 38785094139697029053093797030280437291228399875653.959648

Upvotes: 2

Related Questions