2017-01-21 14 views
5

私はGCCでquadmathライブラリを使用しようとしています。私は対応する四倍精度複素数、__complex128に型キャストしたいと思います。私は交換してみた場合std :: complexをタイプキャストする<double> to __complex128

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

:私は

g++ test.cpp -lquadmath -o test 

でこのコードをコンパイルしようとすると、私は次のエラーを取得する

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

:以下は、最小限の(非)-working例です明示的な型キャストの割り当て行、

y = (__complex128) x; 

私は

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

どのように1は、これらの2つのタイプの間の変換ず、同様のエラーを取得しますか?

答えて

2

私はあなたがあなたの__complex128の個々のコンポーネントを設定する__real____imag__拡張機能を使用することができ、その場合にはGCCを、使用していると思います:

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

これは__complex64のためにクランで動作し、あまりにも(クランdoesnのをまだ__complex128をサポートしていません)。

+0

ありがとうございました!それがトリックでした。私は__real__と__imag__拡張については知らなかった。ニフティ。 – Jeff

0

__complex__はかなり古く(https://gcc.gnu.org/onlinedocs/gcc/Complex.htmlを参照してください)と言うことができるので、私はここにいくつかの種類の互換性の問題があると仮定しなければなりません。この問題をハッキングする方法として、次のように試してみることができます。

y = 1.0i; 
y *= x.imag(); 
y += x.real(); 
関連する問題