Search code examples
c++floating-pointcomplex-numbersmultiprecision

Type conversion c++, quadmath


The quadmath library does not have much documentation online and I would like to perform two simple actions using __complex128 type.

  • Is there a way to easily print the value inside a variable of this type?
  • How can I convert back a variable from __complex128 to complex<double>, or even the single __float128 real and imaginary parts to double?

The second question is more important because if I can convert it I can simply use cout!

EDIT: Everything comes to the following question. How do I convert __float128 to double?


Solution

  • Converting a __float128 to a double does not require anything special:

    const __float128 x{};
    const double y = x;
    

    You might want a static_cast for clarity (and less warnings).

    Constructing a std::complex<T> from __complex128 is also not difficult:

    auto bar(__complex128 y)
    {
        return std::complex<double>{
            static_cast<double>(__real__ y),
            static_cast<double>(__imag__ y)
        };
    }