本文介绍了如何使用复数"i"?在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在编码一个简单的DFT算法,我想在复指数中使用复数i.我看到有人使用#include<complex>#include<cmath>,然后他们使用了重载的符号I(例如exp(2*I)).但似乎在我的Visual Studio编译器中不起作用.那么,有人可以举一个使用复指数的简单例子吗?谢谢!

I am coding a simple DFT algorithm now and I want to use the complex number i in complex exponential. I saw somebody use #include<complex> and #include<cmath>, and then they used the overloaded symbol I such as exp(2*I) . But it seems it doesn't work in my visual studio compiler. So, can anyone give a simple example of using complex exponential? Thanks!

推荐答案

下面是一个简短的完整示例:

Here is a short complete example:

#include <iostream>
#include <complex>
#include <cmath>

using namespace std;
typedef complex<double> dcomp;

main() {
  dcomp i;
  dcomp a;
  double pi;
  pi = 2 * asin(1);
  i = -1;
  i = sqrt(i);
  a = exp(2*pi*i);
  cout << "i is " << i << "and Euler was right: e(i pi) = " << a << endl;
} 

经过g ++测试

这篇关于如何使用复数"i"?在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 09:42