8

Using the complex class and library, how do I assign a complex number to a variable?

I understand that I can set the value when I first instantiate the complex number.
I also understand that I can assign one instantiated complex number to another.
How can I directly assign a complex number to a variable?

Reference:
http://www.cplusplus.com/reference/complex/complex/operators/

Example:

#include <iostream>
#include <complex>

int main() {
    complex<double> a(1.2,3.4), b;
    cout << a; //-> (1.2,3.4)
    b = a;
    cout << b; //-> (1.2,3.4)
    b = (1.2,3.4);
    cout << b; //-> (3.4,0) <-- what the heck is this??
    return 0;
}
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
kmiklas
  • 13,085
  • 22
  • 67
  • 103
  • 1
    Look up the comma operator. – Ed Heal Mar 23 '16 at 03:58
  • it will printed in this manner only see this (_The format in which they are formatted for output insertion is (real,imag)_) from your refernce. [http://www.cplusplus.com/reference/complex/complex/operators/] –  Mar 23 '16 at 04:18

4 Answers4

10

For (1.2,3.4), built-in comma operator will be called.

In a comma expression E1, E2, the expression E1 is evaluated, its result is discarded, and its side effects are completed before evaluation of the expression E2 begins (note that a user-defined operator, cannot guarantee sequencing).

The type, value, and value category of the result of the comma expression are exactly the type, value, and value category of the second operand, E2. If E2 is a temporary, the result of the expression is that temporary. If E2 is a bit-field, the result is a bit-field.

It means 1.2 will be evaluated and then discarded, at last 3.4 is returned.

So b = (1.2,3.4); is equivalent to b = 3.4;, which will call std::complex::operator=(const T& x):

Assigns x to the real part of the complex number. Imaginary part is set to zero.

That's why you get the result of (3.4,0).

As @paddy suggested, you could solve the issue like:

b = {1.2,3.4};                 // copy-list-initialization (since c++11)
b = complex<double>(1.2, 3.4); // copy assignment via a temporary complex<double>
b = decltype(b)(1.2, 3.4);     // same as the above (since c++11)
Community
  • 1
  • 1
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • 4
    This explains the behaviour but doesn't answer the question. Perhaps suggest that the fix is to use `b = complex(1.2, 3.4);` or even `b = decltype(b)(1.2, 3.4);` – paddy Mar 23 '16 at 04:04
7

Try this, you can use b={1.2, 3.4} to assign a complex value

#include <complex>
#include <iostream>
using namespace std;
int main()
{
    complex<double> a = {1,2};
    complex<double> b;

    b = {1.2, 3.4};

    cout << a + b << "\n"; // (2.2,5.4)

}
Jay
  • 601
  • 6
  • 17
4

The way you used your assignment operator = for the std::complex class only assigned the real part of the complex number.

So b = (1.2,3.4); is equivalent to changing the real part of b to 3.4 only.

To assign a complex number directly I think you can use b = complex<double>(1.2,3.4);

Lincoln Cheng
  • 2,263
  • 1
  • 11
  • 17
  • 1
    Watch your wording. You can't say that the "assignment operator only assigns the real part", and then provide a solution that uses the assignment operator correctly. There is more than one assignment operator for `std::complex`. See http://en.cppreference.com/w/cpp/numeric/complex/operator%3D – paddy Mar 23 '16 at 04:13
1

I just wanted to get in on the act with this bad boy, compliments of C++14:

#include <iostream>
#include <complex>

int main() {
    using namespace std::literals;
    std::complex<double> b;
    b = 1.2 + 3.4i;
    std::cout << b << "\n";
}
Christopher Oicles
  • 3,017
  • 16
  • 11