Link to home
Start Free TrialLog in
Avatar of Wraith10
Wraith10Flag for Canada

asked on

solving magnitudes with complex numbers

I am trying to create a class to represent complex numbers and then use it to perform various operations (adding, subtracting, multiplying etc). I am having trouble with the magnitude function.

The code I have for magnitude is as follows:

public double magnitude(){
   Math.sqrt(real^2 + imaginary^2);
}

**Upon compile the above gives the error: operator ^ cannot be applied to double,double**

Then I want to test if the *calling* object has a greater magnitude than the object specified in the parameter (in this case Complex op2) using this:

public boolean greaterThan (Complex op2){
   if (this.magnitude>op2.magnitude)
    return true;
   else
    return false;
}
**Upon compile the above gives the errors: Cannot resolve symbol (^ point to this), Cannot resolve symbol (^ pointing to the . after op2)**

I am extremely hesitant about the code in the second half of the question. I am not sure if I can use the this property that way or not or if I have it backwards.

Some of the initial setup of my class in case it is applicable.

public class Complex {
public double real, imaginary;

public complex(double r, double i){
real=r; imaginary=i;
}


Anyone have any idea?
SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Math.sqrt(real^2 + imaginary^2);

Math.sqrt(real*real + imaginary*imaginary);

Try

public double magnitude(){
   Math.sqrt(Math.pow(real, 2.0) + Marth.pow(imaginary, 2.0) );
}
>>Then I want to test if the *calling* object has a greater magnitude than the object specified in the parameter

Make the class implement Comparable
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think that should have been:

   return (int)(magnitude() - otherComplex.magnitude());

;°)
>>Math.sqrt(Math.pow(real, 2.0) + Marth.pow(imaginary, 2.0) );

For a simple square, it's probably an unnecessary expense to call these functions
>>I think that should have been: ...

I was thinking that magnitude was an attribute. You're right
Hi Wraith10,
u can also make a function to simulate ^ like this:

public double getToThePower(double d, int power)
{
 int inc = power;
 double powered = 1;
  while (inc > 0)
  {
   powered = powered * d;
   inc--;
  }
  return powered
}

Cheers!
>> For a simple square, it's probably an unnecessary expense to call these functions
Right.
But now the author at least knows the existence of Math.pow() ;°)
Avatar of shketinet
shketinet

Hi Wraith10,
^ operator can not be applied to double, that's right.
This operator means
1) Logical XOR if applied to boolean operands
    x ^ y returns true if and only if one operand is true; otherwise, false.
2) Bitwise XOR if applied to integer operands.
The binary bitwise operators perform bitwise operations between corresponding individual bit values in the operands. The result is a new integer value of the promoted type, which can only be either int or long.
A ^ B returns 1 if and only if one of the bits is 1; otherwise, 0.

just replace it with real*real + imaginary*imaginary
 
Cheers!
compareTo would be useful for using your class in collections. If you just want a simple 'greater than', you can do

boolean greaterThan = (c1.compareTo(c2) > 0);
Avatar of Wraith10

ASKER

Using your feedback I've changed my real^2 and imaginary^2 into real*real and imaginary*imaginary. I can't believe I didnt think of doing that I just panicked when it didnt work. I also implemented the  if (this.magnitude()>op2.magnitude()) change that was suggested.

I appreciate the other suggestions that were made but for now those appear to be the simplest corrections. No worries I will PAQ this question once I get the test harness written in no more than a couple of days but I want to leave it open for now in case I encounter unexpected errors.
No problem
All right
Not quite sure why that one answer gets *all* the points but ...
Hi Wraith,

Do you really only want to award my comment? Or did you also want to award one of CEHJ's?
If you made a mistake, you (or we) can always ask a moderator to reopen this question.
Once reopened you can reaccept by splitting the points among the two of us.
Let us know.
I didnt know it was possible (or how) to do that.
Just post a 0 point question to https://www.experts-exchange.com/Community_Support/ and they'll reopen it
8-)

and again, kudos to zzynx for his spirit of fair play
:°) That's the spirit I experience in your comments too, CEHJ.