Home > database >  Boost multiprecision cpp_float not working properly when not using cpp_bin_float_(50/100)
Boost multiprecision cpp_float not working properly when not using cpp_bin_float_(50/100)

Time:11-21

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

CodePudding user response:

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
  • Related