Reputation: 173
I'm trying to use the quadmath library in GCC. I have a complex double value I'd like to typecast into the corresponding quad precision 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 compiling 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 replacing the assignment line with an explicit type cast,
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 does one convert between these two types?
Upvotes: 7
Views: 1670
Reputation: 179412
I guess you're using GCC, in which case you can use the __real__
and __imag__
extensions to set the individual components of your __complex128
:
__complex128 y;
__real__ y = x.real();
__imag__ y = x.imag();
This works in Clang for __complex64, too (Clang doesn't yet support __complex128).
Upvotes: 3
Reputation: 79
I have to assume there's some kind of type compatibility issue here, since as far as I can tell __complex__
is pretty ancient (see https://gcc.gnu.org/onlinedocs/gcc/Complex.html). As a way to hack around this issue, you could try:
y = 1.0i;
y *= x.imag();
y += x.real();
Upvotes: 0