Complex Numbers

I have an application that works with complex number. It simply asks the user to input 3 numbers (magnitude and phase for all 3) and then performs a computation. However it computes the incorrect result.

When I perform the same computation by hand or using Matlab I get a zero phase for the result. But using the C++ code I get a phase of 2.02 degrees. Is there a problem with the C++ class that could cause this?

1
2
3
4
a=polar ( 1.0, 120 * PI / 180 );
a2=polar ( 1.0, 240 * PI / 180 );

Result = (First_Complex + (a2*Sec_Complex) + ( a*Third_Complex)) / 3.0;


To test my code I input 3 values: 93<0, 93<-120, 93<120

These three put through that computation should give a result of ~0 magnitude and exactly 0 phase.

Edit for cleanup.
Last edited on
Hm...
There's either a problem with polar() or with operator*(). Post those two.
I am sorry I guess I should have stated that I am using the built in <complex> class.
1
2
3
#include <complex>

typedef std::complex<double> dcmplx;


So the polar and the * operator are defined within that context. Or am I missing something?
Since the * and / same operator precedence/priority, may be it is calculating as not desired.
In terms of formula (which I am not familiar with), is it 120*PI / 180 or 120 * PI/180 ??
Try enclosing them in brackets and run again.
Like:

a = polar (1.0, (120*PI)/180)
or
a = polar (1.0, 120*(PI/180))

Good luck :)
Just a note to this, I am looking at the result in polar form using abs() and arg(). The abs is returning the correct result but the arg() is returning 2.02 rad. This cooresponds to 115.74 degrees. Not exactly zero as it should be.
The brackets segmenting the operator in the polar call does not change the result. The formula is just a conversion from degrees (120) to rad which is: degrees * PI / 180. The order of the division and multiplication does not change the resultant.
Well, the compiler wouldn't let me do the scalar division, but here's what I got:
(-207.846,-1.0425e-13)
-207.846 is roughly -0.501

1
2
3
std::complex<double> v0(93,0),v1(93,120),v2(93,-120);
std::complex<double> x=std::polar(1.0,_1THIRD_TURN),y=std::polar(1.0,_2THIRD_TURN);
std::complex<double> res=(v0+x*v1+y*v2);
Topic archived. No new replies allowed.