C C ++中的复杂数字? [英] C Complex Numbers in C++?
问题描述
以下代码在C中编译和运行(至少根据'gcc -std = gnu99'),但是无法在C ++下编译,给出第5行:错误:无法将'double'转换为'double 复杂。是否有人知道为什么?
The following code compiles and runs just fine in C (at least according to 'gcc -std=gnu99'), but it fails to compile under C++, giving "line 5: error: cannot convert 'double' to 'double complex' in initialization". Does anybody know why?
#include "/usr/include/complex.h"
#include <stdio.h>
int main(int argc, char * argv[]) {
double complex a = 3; // ERROR ON THIS LINE
printf("%lf\n", creal(a));
return 0;
}
我知道还有另一种方法在C ++中做复杂的数字,在C ++中使用C复数,因为这是我给予的遗留代码做事情。感谢您的帮助!
I realize there is another way of doing complex numbers in C++, but I have to use C complex numbers in C++, because that is how the legacy code I was given does things. Thanks if you can help!
推荐答案
使用C关键字复杂:_Complex。 C ++使用复杂作为(模板)类。
我不确定creal是在哪里,或者我会取消注释。
Use the C keyword for complex: _Complex. C++ uses complex as a (template) class. I'm not sure where creal is or I would uncomment that.
#include <complex.h>
#include <cstdio>
int main(int argc, char * argv[]) {
double _Complex a = 3.0 + 0.0I; // DECLARATION WORKS NOW - NOTE ASSIGNMENT MUST HAVE IMAG PART
//printf("%lf\n", creal(a));
return 0;
}
这适用于gcc(使用g ++编译)。我收到了关于已弃用的.h标头的警告。
This works in gcc (I compiled with g++). I got a warning about deprecated .h headers.
在这里是一个电子邮件链接的链接,显示与C ++和C与复数的非标准兼容性。 C ++ 11需要C ++复合体与C _Complexes的布局兼容性。
Here is a link to an email trail showing nonstandard compatibility with C++ and C with complex numbers. C++11 requires layout compatibility of C++ complexes with C _Complexes.
我目前正在调查C ++中的creal等。我没有在标准中找到任何东西。由于似乎有一些努力在C ++和C之间提供一些源兼容性,那么creal,cpow等可能会成为TR2库建议的一个很好的补充。
I'm currently investigating creal, etc. in C++. I'm not finding anything in the standard. Since there seems to be some effort to provide some source compatibility between C++ and C then creal, cpow, etc. might make a nice addition to TR2 the library proposals.
这篇关于C C ++中的复杂数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!