我正在尝试使用 C 计算 [e^i6(theta)]^2
的值。我只是展示了我的几行代码。我已按照此答案 ( How to work with complex numbers in C? ) 的建议添加了额外的头文件:
#include <stdio.h>
#include <math.h>
float theta;
float dist,xcoord,division;
fprintf(fs,"%f\t%f\t%f\t%f\t%f\n",
dist, xcoord, division,
6*theta, pow(exp(I*6*theta),2));
我的输出是:
1.00000
我的输出文件的前 3 行:
94.214905 68.130005 0.723134 4.574803 1.000000
107.493179 -33.500000 -0.311648 11.326338 1.000000
120.586807 52.529999 0.435620 6.720418 1.000000
我认为这是不正确的。我可以包括什么来让它发挥作用?我同意我可以在 cos 和 sin 中分解这个公式,但我正在寻找一个直接的选择。
最佳答案
你应该使用 <complex.h>
而不是 <math.h>
.
I
仅在 complex.h 中定义。应该在编译时触发警告
另外,如前所述,使用复杂函数:
printf("I = %f%+fi\n", crealf(I), cimagf(I));
z = clog(cexp(I * 6.0 * theta));
printf("z = %f%+fi\n", crealf(z), cimagf(z));
输出:
I = 0.000000+1.000000i
z = 0.000000-1.708382i
不要忘记链接时的数学库:-lm
关于使用c编程的复数定理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40436880/