PLEASE HELP: converting complex numbers to polar form

Please could someone write me a script that can multiply and divide complex numbers and give the answer in polar form, it needs to be a menu screen in which you can enter any two complex numbers and receive a result in polar form, you'd really be helping me out.
Last edited on
complex <double> c;
complex <double> d;
complex <double> r;

r = c/d; //division example, obviously need to put in values for both c & d
r = c*d; //multiply example

and finally...
cout << polar(r.real(), r.imag());


you can see more including some better examples (these are really just telling you its there to use) on

https://www.geeksforgeeks.org/complex-numbers-c-set-1/

side note, c++ isnt a scripted language.
Last edited on
Do you know how to multiply and divide complex numbers on paper, but not in code? Explain the process to yourself, and go through some examples, step by step, until you can think of a series of steps that you can translate into individual operations in code.

(a + bi)*(c + di) = ??? (figure out what is this in math language first, and then translate it into C++)

You're trying to turn (a + bi) -> r * e^(i theta).
Can you explain how to do that in English/math first?

Note: The standard library offers functions such as std::exp(x) for calculator e^x in <cmath>. It also has std::arg(x)
http://www.cplusplus.com/reference/complex/arg/
1
2
3
4
5
6
7
8
9
10
11
12
13
// arg complex example
#include <iostream>     // std::cout
#include <complex>      // std::complex, std::abs, std::arg

int main ()
{
  std::complex<double> mycomplex (3.0,4.0);

  std::cout << "The polar form of " << mycomplex;
  std::cout << " is " << std::abs(mycomplex) << "*e^i*" << std::arg(mycomplex) << "rad\n";

  return 0;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <complex>

int main()
{
    // http://en.cppreference.com/w/cpp/numeric/complex
    using namespace std::literals ;

    const std::complex<double> c = 17.0 + 25i ;

    // convert complex to polar
    const double r = std::abs(c) ;
    const double theta = std::arg(c) ;

    // convert polar (magnitude and phase) to complex number
    const std::complex<double> c2 = std::polar( r, theta ) ;

    std::cout << std::fixed << c << " == " << c2 << '\n' ; // (17.000000,25.000000) == (17.000000,25.000000)
}

http://coliru.stacked-crooked.com/a/ae8d231cea23fe8f
thank you for your responces, all are helpful but what im looking for is a script that when the program runs the user can enter any two complex numbers, choose whether to multiply or divide them and recieve the answer in polar form, i am very new to programming and have no clue how to do this haha really be helping me out!
the sample programs on the site I linked HAVE most of what you need in the short sample programs. Was there anything missing in those?

JL's code is pretty much everything except reading the values in ... a couple of cin statements.

just add

cout << "Enter the complex number \n"
cin >> c; //user types real, imaginary I don't think the i is required but can't recall.

on line 10. It won't validate the data but it gets the job done if you are ok with GIGO.
Last edited on
Topic archived. No new replies allowed.