1

I need to do calculations with higher precision than doubles and am using boost::multiprecision for that. This works perfectly fine when I use boost::multiprecision::cpp_bin_float_50 or boost::multiprecision::cpp_bin_float_100. So simply doing something like

#include <boost/multiprecision/cpp_bin_float.hpp>
// ...

boost::multiprecision::cpp_bin_float_100 Test1, Test2;
Test1 = 1.0;
Test2 = 2.0;
Test1 = Test1 + Test2;

works. But I need different amounts of precision. So for example, I'd simply like to do something like

boost::multiprecision::cpp_bin_float<200> Test1, Test2;
Test1 = 1.0;
Test2 = 2.0;
Test1 = Test1 + Test2;

While the first three lines work fine, I get an error in the forth line saying "no matching operator found". Also .convert_to<double>() is not found, which I will need later. Same thing for cpp_dec_float...

I'm sure, I am missing something stupid here. Can anybody help?

Edit:

Thank you sehe - I just wanted to edit in the exact same thing. It took me ages to find that out. Funny how I couldn't find one single example of how to use arbitrary lengths other than ..._50 or ..._100...

Paul Aner
  • 361
  • 1
  • 8
  • To the close voters, please be (way) more careful. This question did *not* require additional debugging details. In fact, if code as posted doesn't compile there's no possible way to debug anything. To OP: you can save yourself the trouble by making the code self-contained. Just add the 3 lines to `#include` a header and declare `int main()`... – sehe Nov 20 '22 at 19:49

1 Answers1

1

The boost::multiprecision::cpp_bin_float<200> type is a backend type. You want a frontend type, which would be e.g.

number<cpp_bin_float<200> >

You can compare this with the definitions of the working types:

using cpp_bin_float_50 = number<backends::cpp_bin_float<50> > ;
using cpp_bin_float_100 = number<backends::cpp_bin_float<100> >;

Backends define the implementation, where the frontend provides the usability interfaces that you expect, like operator overloads.

Live Demo

#include <boost/multiprecision/cpp_bin_float.hpp>
namespace bmp = boost::multiprecision;

int main() {
    bmp::number<bmp::cpp_bin_float<200>> v(0);

    v += 2.0;
    v = pow(v, 156);

    std::cout << std::fixed << v;
}

Prints:

91343852333181432387730302044767688728495783936.000000
sehe
  • 374,641
  • 47
  • 450
  • 633