Typecasting std :: complex <double> to __complex128

I am trying to use the quadmath library in GCC. I have a complex double value that I would like to lead to the corresponding quadratic complex number, __complex128 . The following is a minimal (non) working example:

 #include <quadmath.h> #include <complex> #include <stdio.h> using namespace std::complex_literals; int main(){ std::complex<double> x = 1 + 2i; std::printf("x = %5.5g + %5.5g\n", x.real(), x.imag()); __complex128 y = 2+2i; y = x; return 0; } 

When I try to compile this code with

  g++ test.cpp -lquadmath -o test 

I get the following error:

 test.cpp:10:6: error: cannot convert 'std::complex<double>' to '__complex128 {aka __complex__ __float128}' in assignment y = x; 

If I try to replace the target string with an explicit type,

 y = (__complex128) x; 

I get a similar error

 test.cpp:10:21: error: invalid cast from type 'std::complex<double>' to type '__complex128 {aka __complex__ __float128}' y = (__complex128) x; 

How to convert between these two types?

+5
source share
2 answers

I assume that you are using GCC, in which case you can use the __real__ and __imag__ to install the individual components of your __complex128 :

 __complex128 y; __real__ y = x.real(); __imag__ y = x.imag(); 

This also works in Clang for __complex64 (Clang does not yet support __complex128).

+2
source

I should assume some kind of type compatibility problem here, since, as far as I can tell, __complex__ quite ancient (see https://gcc.gnu.org/onlinedocs/gcc/Complex.html ). As a way to crack this problem, you can try:

 y = 1.0i; y *= x.imag(); y += x.real(); 
0
source

Source: https://habr.com/ru/post/1263129/


All Articles