2017-01-21 47 views
5

我正在尝试在GCC中使用quadmath库。我有一个复杂的双精度值,我想要输入相应的四精度复数,__complex128。以下是最小的(非)木材加工例如:Typecasting std :: complex <double> to __complex128

#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; 
} 

当我尝试编译此代码

g++ test.cpp -lquadmath -o test 

我得到以下错误:

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

如果我尝试更换具有明确类型转换的分配线,

y = (__complex128) x; 

我收到了类似的错误

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

一个人怎么这两种类型之间的转换?

回答

2

我猜你使用GCC,在这种情况下,你可以使用__real____imag__扩展设置的各个组件的__complex128

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

这适用于铛为__complex64,太(锵没有按还没有支持__complex128)。

+0

谢谢!那就是诀窍。我不知道__real__和__imag__扩展名。俏皮。 – Jeff

0

我不得不假设这里存在某种类型的兼容性问题,因为据我所知__complex__是相当古老的(参见https://gcc.gnu.org/onlinedocs/gcc/Complex.html)。作为解决此问题的一种方法,您可以尝试:

y = 1.0i; 
y *= x.imag(); 
y += x.real(); 
相关问题