I need to write a c++ segment to calculate the phase angle of a complex number on the interval [0, 2pi). The segment needs to return -1 when the phase is undefined. I am having trouble figuring out how to adjust a number outside of the interval and get it into the interval (i.e. add 2pi, add pi, etc). This is what I have so far:
if (real == 0 && imag==0)
{
return -1;
}
else if (real > 0 && imag < 0)
{
return ((2*M_PI) + atan2(imag,
real));
}
else
{
return atan2(imag, real);
}
but it is not passing my instructor's tests
Just use: if (real == 0 && imag==0) { return -1; } else if (atan2(imag, real) < 0) { return ((2*M_PI) + atan2(imag, real)); } else { return atan2(imag, real); }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
Get Answers For Free
Most questions answered within 1 hours.