0

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,

Community
  • 1
  • 1
Joe C
  • 2,757
  • 2
  • 26
  • 46

1 Answers1

2

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
sehe
  • 374,641
  • 47
  • 450
  • 633
  • gmp_float requests a special backend. The header-based cpp_dec_float does not seem to support the template argument to be 0? – Joe C Mar 08 '16 at 01:11
  • 1
    I don't think GMP's backend is special. See also: http://stackoverflow.com/questions/33849373/how-to-change-at-runtime-number-precision-with-boostmultiprecision/33849610#33849610 – sehe Mar 08 '16 at 01:15
  • Thank you! http://stackoverflow.com/questions/16033578/how-to-convert-from-cpp-dec-float-50-to-cpp-int-and-regarding-floating-points-i says the same thing. I also learned that the computation cost is related to precision. I think I'd better not to use unlimited precision if possible... – Joe C Mar 08 '16 at 02:23