本文介绍了类型转换std :: complex< double>去__complex128的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在GCC中使用quadmath库。我有一个复杂的double值,我想将其转换为相应的四元精度复数, __ complex128 。以下是一个最小(非)工作的例子:

  #include< quadmath.h> 
#include< complex>
#include< stdio.h>
使用namespace std :: complex_literals;

int main(){
std :: complex< double> x = 1 + 2i;
std :: printf(x =%5.5g +%5.5g\\\
,x.real(),x.imag());
__complex128 y = 2 + 2i;
y = x;
返回0;

$ / code>

当我尝试使用

  g ++ test.cpp -lquadmath -o test 

我得到以下错误:

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

如果我尝试用明确的类型转换替换赋值行,

  y =(__complex128)x; 

我得到一个类似的错误

  test.cpp:10:21:error:从类型'std :: complex< double>'类型转换为'__complex128 {a_ __complex__ __float128}'
y =(__complex128)x ;

如何在这两种类型之间转换?

解决方案

我猜你正在使用GCC,在这种情况下,你可以使用 __ real __ __ imag__ 扩展程序来设置 __ complex128 的单个组件:

  __ complex128 y; 
__real__ y = x.real();
__imag__ y = x.imag();

这也适用于__complex64的Clang(Clang还不支持__complex128)。 >

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?

解决方案

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).

这篇关于类型转换std :: complex&lt; double&gt;去__complex128的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 17:40