使用c编程的复数定理

标签 c complex-numbers

我正在尝试使用 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/

相关文章:

python - 在复平面上绘制图形

java - 使用 HashSet 的复数

python - 将 3D 图的轴放置在图表内

matlab - 在 MATLAB 中数值计算复值函数的导数

c - 为什么将指针分配给多维数组时得到 "incompatible pointer type"

c - 如何计算范围号 1 到 n 中 0 或 1 的总出现次数?

c - C 语言的 SHA-512 源代码

c - 简单的 C 程序,未知的输出

c - 如何在makefile中包含静态库

c++ - typeid(复杂<double>(0.0,1.0)) != typeid(1.0i)