How to define Complex variables in C++ and Use of Header file

Answered How to define Complex variables in C++ and Use of Header file

  • Tuesday, December 20, 2011 4:54 PM
     
     
    Could someone help me on complex variables: complex(a,b), in C++, as to what to use in the header files in Visual Studio 2008, and input and output these complex # to a file?

All Replies

  • Tuesday, December 20, 2011 7:55 PM
     
     Answered
    Could someone help me on complex variables: complex(a,b), in C++, as to what to use in the header files in Visual Studio 2008, and input and output these complex # to a file?

    To use std::complex template in C++ you have to #include <complex> header file.

    Then you can use complex<double> or complex<float> basing on the precision you require:

    #include <complex>

    using std::complex;

    complex<double> z1(10.0); // 10 + 0i)

    complex<double> z2(-3.0, 4.0) // -3 + 4i

    ...

    To input/output complex numbers to a file, you may want to just input/output their real and coefficient of imaginary part separately.

    Giovanni

     

  • Wednesday, December 28, 2011 2:07 AM
    Moderator
     
     
    Hi Good_Fellow,
     
    I'd like to mark Giovanni's reply as answer. If you have any questions, you can post back and unmark it. We'll continue working with you on this issue.
     
    Thanks for your understanding.
    Have a nice day!
    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us