如何在 C++ 中使用复数“i”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17925904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:34:21  来源:igfitidea点击:

How to use complex number "i" in C++

c++overloadingcomplex-numberscomplextype

提问by Cancan

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 Isuch 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!

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

采纳答案by Floris

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;
} 

Tested with g++

用 g++ 测试

回答by Mike22LFC

I get this question recently as well and find a easy way for future reader:

我最近也收到了这个问题,并为未来的读者找到了一个简单的方法:

Just use <complex>library like the following

只需使用<complex>如下所示的库

#include <iostream>
#include <complex>
using namespace std ;

int main(int argc, char* argv[])
{
    const   complex<double> i(0.0,1.0);    
    cout << i << endl ;

    return(0) ;
}

回答by ogni42

You can find details here

你可以在这里找到详细信息

A simple approach would be

一个简单的方法是

#include <complex>

using std::complex;
const double pi = 3.1415;
void foo()
{
    complex<double> val(polar(1, pi/2.0); Create a complex from its olar representation
}

回答by Ayxan

Another way is to use std::literals::complex_literals::operator""iafter C++14:

另一种方式是std::literals::complex_literals::operator""i在C++14之后使用:

#include <iostream>
#include <complex>

int main() {
    using namespace std::complex_literals;
    auto c = 1.0 + 3.0i;
    std::cout << "c = " << c << '\n';
}

Output:

输出:

c = (1,3)

回答by Paul Mackenzie

The following code in C++ shows a macro for implementing the imaginary number j. It is well known that in programming the terms i and j are commonly used as counter variables. I instead use the capital letter J to represent the imaginary number to avoid any confusion.

以下 C++ 代码显示了一个用于实现虚数 j 的宏。众所周知,在编程中,术语 i 和 j 通常用作计数器变量。我改为使用大写字母 J 来表示虚数以避免任何混淆。

/ * dcomplex.h

/* dcomplex.h

#ifndef DCOMPLEX_H_
#define DCOMPLEX_H_
#define J dcomplex(0.0,1.0)
typedef std::complex<double> dcomplex;
#endif /* DCOMPLEX_H_ */

Using this macro, the imaginary number J [together with the complex library] can be used in the main code. An example of its use is shown below:

使用这个宏,可以在主代码中使用虚数 J [与复数库一起]。其使用示例如下所示:

....
....
#include <complex>
#include "dcomplex.h"

....
....
 tmp = tmp + t[n]*exp( (2.0*PI*(double)n*(double)l/(double)tab_size)*J );
....

....

....

where tmp, t[n] are variables of a complex type, and J is the imaginary number. The variables n, l, and tab_size are of an integer type. The constant PI is the well known constant 3.14... The function exp() is overloaded to handled complex numbers. [n.b. this code sample is part of a simple DFT]

其中 tmp、t[n] 是复数类型的变量,J 是虚数。变量 n、l 和 tab_size 是整数类型。常数 PI 是众所周知的常数 3.14... 函数 exp() 被重载以处理复数。[nb 此代码示例是简单 DFT 的一部分]

Using this macro, the code is more readable..

使用这个宏,代码更易读..

回答by user3405743

pi, being an irrational number, cannot be exactly represented by a double. cosof an inexact approximation of pi is likely to yield a result which is close to but perhaps not exactly 1. Likewise sinof an inexact approximation of an inexact approximation of piis like to result in a number is has a very small magnitude that is perhaps not exactly 0. Why not just define I to be std::complex(0.0, 1.0)and avoid the gratuitous inexactness.

pi是一个无理数,不能用double精确表示。 pi 的不精确近似值的cos可能会产生接近但可能不完全是 1 的结果。同样,pi的不精确近似值的不精确近似值的sin就像导致一个数字的大小非常小,即也许不完全是 0。为什么不将 I 定义为std::complex(0.0, 1.0)并避免无端的不精确。