1

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?

NSZ
  • 215
  • 1
  • 7
  • These links might help: [\[1\]](https://gcc.gnu.org/ml/gcc/2016-07/msg00120.html) [\[2\]](https://stackoverflow.com/a/41776202/3282436) – 0x5453 Jun 27 '17 at 13:28
  • Thank you for the links but it gives me an error: undefined reference to quadmath_snprintf. However I need mostly the conversion – NSZ Jun 27 '17 at 13:37
  • @NSZ: That's s linker error. A wild guess would be that you have to link with libquadmath (try `-lquadmath`). – You Jun 27 '17 at 15:20
  • @You: Thanks for the answer. Sorry for my newbeness, but where do I put this command ( I am on windows). – NSZ Jun 27 '17 at 15:48
  • @NSZ: Wherever linker flags go, which depends on what compiler/build system you use. – You Jun 27 '17 at 16:32

2 Answers2

2

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)
    };
}
You
  • 22,800
  • 3
  • 51
  • 64
0

Try this:

std::complex<double> x;
__complex128 y;
x.real(__real__ y)
x.imag(__imag__ y)
gsamaras
  • 71,951
  • 46
  • 188
  • 305