#Returning an object changes the values again
61 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
!f
Phasor Phasor::operator*(const Phasor& rSide) {
double resultMagnitude = magnitude * rSide.magnitude;
double resultPhase = phase + rSide.phase;
// Ensure the result phase is positive and measured counter-clockwise
if (resultPhase < 0) {
resultPhase += 360.0;
}
while (resultPhase >= 360.0) {
resultPhase -= 360.0;
}
cout << "Debug - magnitude1: " << magnitude
<< ", magnitude2: " << rSide.magnitude << endl;
cout << "Debug - phase1: " << phase << ", phase2: " << rSide.phase << endl;
cout << "Debug - resultMagnitude: " << resultMagnitude
<< ", resultPhase: " << resultPhase << endl;
// Return a temporary Phasor object without modifying the current object
return Phasor(resultMagnitude, resultPhase);
}
!f
Phasor::Phasor(double iMag, double iPhase) {
// Calculate the magnitude
double temp = sqrt(pow(iMag, 2) + pow(iPhase, 2));
// Calculate the phase in degrees
double tempPhase = atan2(iPhase, iMag) * (180.0 / Pi);
// Ensure the phase is always positive and measured counter-clockwise
while (tempPhase < 0) {
tempPhase += 360.0;
}
// Set the magnitude and adjusted phase
magnitude = temp;
phase = tempPhase;
}
the first code is the function that works fine and outputs a correct value that i want, but when it gets to the return, it goes through that constructor again and changes the values
ive been staring at it for like 4 hours so im lost
are you expecting it to return a new Phasor or alter the same instance?
im trying to store the two variables so i can output the result in polar form
=================================
This is MP = P1 * P2
Magnitude: 365.796 Phase: 89.2995
like this result, but the values get messed up
!f
out << "This is MP = P1 * P2" << endl;
cout << "Magnitude: " << setw(15) << left << MP.getMagnitude()
<< "Phase: " << MP.getPhase() << endl;
cout << "=================================" << endl;
Debug - phase1: 315, phase2: 50.7685
Debug - resultMagnitude: 4.47214, resultPhase: 5.76848```
the last line is the expected result
You can just wrap it in
```cpp
```
Maybe you're miscalculating something
This constructor looks wrong
i dont think so, i ran a debuger on that return line, the values are right until the constructor
What are the parameters?
It looks like you're converting Cartesian to polar, but labeling the parameters incorrectly
Phasor P1(sqrt(2.0), -sqrt(2.0));
Phasor P2(sqrt(2.0), sqrt(3.0));```
this what you mean by parameters? im a lil on the dumb side so im not 100% sure what you mean
iMag and iPhase
I would have expected just assigning them directly to magnitude and phase
ill try that but i think last time it gave me an error because of the way phasors are set up
Phasor::Phasor(double iMag, double iPhase)
{
// Ensure the phase is always positive and measured counter-clockwise
while (iPhase < 0) {
iPhase += 360.0;
}
// Set the magnitude and adjusted phase
magnitude = sqrt(pow(iMag, 2) + pow(iPhase, 2));
phase = atan2(iPhase, iMag) * (180.0 / Pi);
}```
somthin like this?
Magnitude is equal to the square root of the magnitude squared plus the phase squared?
yes i believe so
Why?
well, the variable isnt really magnitude
its a place holder for it, its in rectangular for that gets inputed
Then why is it named iMag
honestly, idk o-o'
hmmm, imma try labeling them somethin else and messing around.
How so
Phasor::Phasor(double num1, double inum2)
{
//Calculate the magnitude
double tempMag = sqrt(pow(num1, 2) + pow(inum2, 2));
//Calculate the phase in degrees
double tempPhase = atan2(inum2, num1) * (180.0 / Pi);
//Ensure the phase is always positive and measured counter-clockwise
while (tempPhase < 0) {
tempPhase += 360.0;
}
//Set the magnitude and adjusted phase
magnitude = tempMag;
phase = tempPhase;
}```
You should choose better names
A, B aren't really transparent in terms of what they are for
i honestly can think of anything else other than num1 and num2, and my problem still stands for when i use the overloaded operator, it runs through that constructor again changing the values and going through the instructions
how do i get around that, or am i returning the wrong thing?
Your phasor constructor creates a phasor from the rectangular coordinates, correct?
I mean you're telling it to call the constructor
yes
And what are you passing into that constructor?
a number and an imaginary number
On the last line here
the magnitude and phasor values
Is that what the constructor expects?
I mean num2 doesn't look like an imaginary number to me
you sure you don't want to use something like std::complex?
i dont know how to use that even if i could, in oop rn
uhhh no...
So you can see the problem now, right?
yeah lmaoo, but i dont know what to create to return the values.
What you should do if you intend to use a complex number with double imaginary and real parts is to use std::complex<double>
^^ ty for the help and guiding me to see what i was doin wrong.
Thank you and let us know if you have any more questions!
This thread is now set to auto-hide after an hour of inactivity