Dec 28, 2013 at 11:14am Dec 28, 2013 at 11:14am UTC
I think the std::pow function is defined in the <cmath> standard header.
Also you could post the error for more help.
Edit:: BTW what do you mean by float mod and float angle?
Aceix.
Last edited on Dec 28, 2013 at 11:16am Dec 28, 2013 at 11:16am UTC
Dec 28, 2013 at 11:38am Dec 28, 2013 at 11:38am UTC
You need to add
#include <cmath>
at the beginning of your code to use the pow() function. Also your code gives the wrong answer I believe.
Here is my implementation of complex numbers division:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
#include <iostream>
#include <cmath>
struct complex
{
float a; //The real part
float b; //The imaginary part
};
complex divide(complex ab, complex cd)
{
float real = ((ab.a*cd.a)+(ab.b*cd.b))/(pow(cd.a,2)+pow(cd.b,2));
float imaginary = ((ab.b*cd.a)-(ab.a*cd.b))/(pow(cd.a,2)+pow(cd.b,2));
complex ans = {real, imaginary};
return ans;
}
int main()
{
complex cmplx_1 = {2, 3};
complex cmplx_2 = {4, -5};
complex cmplx_3 = divide(cmplx_1, cmplx_2);
if (cmplx_3.b > 0)
std::cout << "Result: " << cmplx_3.a << "+" << cmplx_3.b << "i" << std::endl;
else if (cmplx_3.b < 0)
std::cout << "Result: " << cmplx_3.a << "-" << cmplx_3.b << "i" << std::endl;
return 0;
}
Last edited on Dec 28, 2013 at 11:44am Dec 28, 2013 at 11:44am UTC
Dec 28, 2013 at 5:01pm Dec 28, 2013 at 5:01pm UTC
Thank you very much for your help.And I have last question.How can I do if I wanted to divide into polar representation of complex numbers ? I think I'll write float angle and float mod but how can I write?
Dec 28, 2013 at 5:20pm Dec 28, 2013 at 5:20pm UTC
Last edited on Dec 28, 2013 at 5:28pm Dec 28, 2013 at 5:28pm UTC