6

I'm using the boost multiprecision library, and more precisely the boost::multiprecision::float128 type. Using ICPC for compiling, I get some errors when trying to to do something like:

double a = functionA();

where functionA() return a boost::multiprecision::float128 variable.

error: no suitable conversion function from "boost::multiprecision::float128" to "double" exists|

How can I solve this?

3
  • What happens if you assign the result of float128_backend?
    – Columbo
    Jan 30, 2015 at 17:50
  • If functionA() returns a boost::multiprecision::backends::float128_backend, I get: error: cannot convert ‘const boost::multiprecision::backends::float128_backend’ to ‘double’ in initialization| Feb 5, 2015 at 18:28
  • It's a member function you have to call.
    – Columbo
    Feb 5, 2015 at 18:29

1 Answer 1

16

From the Boost documentation:

A number can be converted to any built in type, via the convert_to member function:

mpz_int z(2);
double i = z.convert_to<double>(); // sets i to 2

http://www.boost.org/doc/libs/1_57_0/libs/multiprecision/doc/html/boost_multiprecision/tut/conversions.html

1
  • thank you for your answer! Just another detail: since functionA() is a template function, and can returns a double or a boost::multiprecision::float128 variable, is there a way to keep the above syntax "double a = functionA();" but using your converter function? do I need to create a custom cast operator? Feb 3, 2015 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.