user2649077
user2649077

Reputation:

C++ complex numbers, what is the right format?

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?)

Thanks in advance :-)

Upvotes: 17

Views: 35198

Answers (5)

Jerry Coffin
Jerry Coffin

Reputation: 490663

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.

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:

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.

Starting with C++17, the compiler can also deduce the template parameter from the initializer, so you can do something like:

std::complex foo = 1.2 + 3.4i;

...and since 1.2 and 3.4 are both doubles, it deduces that the template parameter also needs to be a double.

Upvotes: 11

vk3who
vk3who

Reputation: 389

Here is an example of how to use . It compiles and runs under 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();
}

Upvotes: 1

Manu343726
Manu343726

Reputation: 14184

The constructor of std::complex has two parameters:

  • 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:

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

Upvotes: 11

Minimus Heximus
Minimus Heximus

Reputation: 2815

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

Upvotes: 8

// 1 + 2i
std::complex<double> c(1, 2);

Upvotes: 17

Related Questions