C++ 复数,正确的格式是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18035525/
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
C++ complex numbers, what is the right format?
提问by
I want to use C++ with complex numbers. Therefore I included #include <complex>
. Now my question is: How do I declare a variable?(so what is the format called for let's say: 1 + i
?)
我想对复数使用 C++。因此我包括#include <complex>
. 现在我的问题是:我如何声明一个变量?(那么我们说的格式是什么:1 + i
?)
Thanks in advance :-)
提前致谢 :-)
采纳答案by Antti Haapala
// 1 + 2i
std::complex<double> c(1, 2);
回答by Manu343726
The constructor of std::complex
has two parameters:
的构造函数std::complex
有两个参数:
- The first, wich has the real part of the number.
- The second, wich has the imaginary part of the number.
- 第一个,它具有数字的实部。
- 第二个,有数字的虚部。
For example:
例如:
std::complex<float> my_complex(1,1); //1 + 1i
Also, C++11 introduces user defined literals, wich allows us to implement (Or be implemented by the standard library, as in this C++14 accepted proposal) a literal for easy-to-use complex numbers:
此外,C++11 引入了用户定义的文字,它允许我们实现(或者由标准库实现,如在这个C++14 接受的提案中)一个易于使用的复数的文字:
constexpr std::complex<float> operator"" i(float d)
{
return std::complex<float>{0.0L,static_cast<float>( d )};
}
You could use this as follows:
您可以按如下方式使用它:
auto my_complex = 1i; // 0 + 1i
回答by nano
Try this:
尝试这个:
#include <complex>
#include <iostream>
using namespace std;
int main()
{
complex<double> a = {1,2};
complex<double> b(3,4);
cout << a + b << "\n";
}
回答by Jerry Coffin
You define a variable by specifying a template parameter and specifying a name for the variable, about like with most other templates:
您可以通过指定模板参数并为变量指定名称来定义变量,与大多数其他模板类似:
std::complex<double> x(1, 1);
The first parameter to the ctor is the real part, the second the imaginary part.
ctor 的第一个参数是实部,第二个是虚部。
Starting with C++ 14, a user-defined literal operator has been added, so you can initialize a complex variable with a somewhat more natural notation:
从 C++ 14 开始,添加了用户定义的文字运算符,因此您可以使用更自然的表示法初始化复杂变量:
using namespace std::literals;
std::complex<double> c = 1.2 + 3.4i;
In this case, (obviously enough) the 1.2
is the real part and the 3.4
is the imaginary part.
在这种情况下,(很明显)the1.2
是实部,the3.4
是虚部。
回答by vk3who
Here is an example of how to use . It compiles and runs under QT
这是一个如何使用的示例。它在 QT 下编译和运行
#include <QCoreApplication>
#include<complex>
#include<iostream>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::complex<double> x=3.0-3.0i;
std::complex<double> y=2.0+4.0i;
cout.precision(3);
cout<<"x="<<x<<" y="<<y<<'\n';
cout<<" OR x real="<<real(x)<<" x imagine="<<imag(x)<<"\n\n";
complex<double> sum = x + y;
cout<<"The sum: x + y = "<<sum<<'\n';
complex<double> difference = x - y;
cout<<"The difference: x - y = "<<difference<<'\n';
complex<double> product = x * y;
cout<<"The product: XY = "<<product<<'\n';
complex<double> quotient = x / y;
cout<<"The quotient: x / y = "<<quotient<<'\n';
complex<double> conjugate = conj(x);
cout<<"The conjugate of x = "<<conjugate<<'\n';
complex<double> reciprocal = 1.0/x;
cout<<"The reciprocal of x = "<<reciprocal<<'\n';
complex<double> exponential =exp(x);
cout<<"The exponential of x = "<<exponential<<'\n';
double magnitude=2.0,phase=45;
cout<<"magintude = "<<magnitude<<" phase = "<< phase<<" degrees\n";
complex<double> pol= std::polar(2.0,(M_PI/180.0)*phase);
cout<<"The polar: x , y = "<<pol<<'\n';
return a.exec();
}