简体   繁体   中英

boost:multiprecision

I have just started using boost::multiprecision trying to speed up some calculations previously done in Matlab. I found quite an unexpected problem, though. My calculations involve complex numbers, so I am using cpp_complex_50 type (eg cpp_complex_50 A, B; )

At some point I need to use boost::math::tools::bracket_and_solve_root() function, which requires that the function it works on returns real values. Here comes my problem... I cannot convert my complex multiprecision variable A.real() to any type that is real, eg. to cpp_dec_float_50 type or even double . The task should be streightforward, but I am virtually drowned in error complaints from my compiler (MSVC2015), and cannot solve it. Any hints at how to convert the data are more than welcome.

A somewhat connected question is the problem of initialization of cpp_complex_50 type variables with real values. At the moment I can only use data of type double at initialization, which means I am loosing some accuracy at the initialization stage already, eg:

cpp_complex_50 A = 4.0 * boost::math::constants::pi<double>(); // it works

but

cpp_complex_50 A = 4.0 * boost::math::constants::pi<cpp_dec_float_50>(); // It does NOT work

Any hints are needed. I am stuck at this, despite nice initial results.

Regards

Pawel

cpp_complex uses cpp_bin_float .

Live On Compiler Explorer

#include <boost/multiprecision/cpp_complex.hpp>
#include <iostream>

namespace bmp = boost::multiprecision;

int main() {
    using Complex = bmp::cpp_complex_100;
    using Real    = Complex::value_type;

    Real r = 4.0 * boost::math::constants::pi<Real>();

    Complex b(r, {});
    // or
    b = r.convert_to<Complex>();

    std::cout << b.str(100) << std::endl;
}

Prints

12.56637061435917295385057353311801153678867759750042328389977836923126562514483599451213930136846827

Following valuable comment from sehe... the code

cpp_complex_50 A = 4.0 * boost::math::constants::pi<cpp_bin_float_50>();
cout << A << endl;

works, producing:

12.5663706143591729538505735331180115367886775975

Similarely,

cpp_bin_float_50 B = A.real();
cout << B << endl;

works as well, printing the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM