#Returning an object changes the values again

61 messages · Page 1 of 1 (latest)

broken token
#

I have a function that calculates the multiplication of phasors and returns two variables back to my phasor class, but when I do so, it changes the values of what im returning and then prints it out. I dont know how to get around returning the values without them being changed.

uneven basinBOT
#

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.

dire pawn
#

!f

uneven basinBOT
#
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);
}
NeonDragon
dire pawn
#

!f

uneven basinBOT
#
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;
}
NeonDragon
broken token
#

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

dire pawn
#

are you expecting it to return a new Phasor or alter the same instance?

broken token
#

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

uneven basinBOT
#
out << "This is MP = P1 * P2" << endl;
cout << "Magnitude: " << setw(15) << left << MP.getMagnitude()
     << "Phase: " << MP.getPhase() << endl;
cout << "=================================" << endl;
NeonDragon
broken token
#
Debug - phase1: 315, phase2: 50.7685
Debug - resultMagnitude: 4.47214, resultPhase: 5.76848```
#

the last line is the expected result

dire pawn
dire pawn
heady summit
broken token
#

i dont think so, i ran a debuger on that return line, the values are right until the constructor

heady summit
#

What are the parameters?

#

It looks like you're converting Cartesian to polar, but labeling the parameters incorrectly

broken token
#
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

heady summit
#

iMag and iPhase

#

I would have expected just assigning them directly to magnitude and phase

broken token
#

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?

heady summit
#

Magnitude is equal to the square root of the magnitude squared plus the phase squared?

broken token
#

yes i believe so

heady summit
#

Why?

broken token
#

well, the variable isnt really magnitude

#

its a place holder for it, its in rectangular for that gets inputed

heady summit
#

Then why is it named iMag

broken token
#

honestly, idk o-o'

heady summit
#

There's your problem

#

Youre labeling a value as something it is not

broken token
#

hmmm, imma try labeling them somethin else and messing around.

dire pawn
broken token
#
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;
}```
dire pawn
#

You should choose better names
A, B aren't really transparent in terms of what they are for

broken token
#

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?

heady summit
#

Your phasor constructor creates a phasor from the rectangular coordinates, correct?

dire pawn
heady summit
#

And what are you passing into that constructor?

broken token
#

a number and an imaginary number

broken token
#

the magnitude and phasor values

heady summit
#

Is that what the constructor expects?

dire pawn
#

you sure you don't want to use something like std::complex?

broken token
#

i dont know how to use that even if i could, in oop rn

broken token
dire pawn
heady summit
broken token
#

yeah lmaoo, but i dont know what to create to return the values.

dire pawn
#

What you should do if you intend to use a complex number with double imaginary and real parts is to use std::complex<double>

broken token
#

^^ ty for the help and guiding me to see what i was doin wrong.

uneven basinBOT
#

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