Square Root of -1

Thread Solved
Reply

Join Date: Sep 2008
Posts: 39
Reputation: ohnomis is an unknown quantity at this point 
Solved Threads: 0
ohnomis ohnomis is offline Offline
Light Poster

Square Root of -1

 
0
  #1
Nov 22nd, 2008
Hello,

I'm writing a a program in C++ that adds/subtracts complex numbers. I'm having trouble writing the square root of -1 or "i" in a complex number.

Complex number:
realPart + imaginaryPart * i (where i is /-1) ["square root of -1"]

so far I'm thinking the code is something like this, but don't know how to go abouts to writing the square root part.

  1. double Complex::plusEq( Complex secondComplex )
  2. {
  3. double totalReal = real + secondComplex.getReal();
  4. double totalImag = imaginary + secondComplex.getReal();
  5.  
  6. return totalReal + totalImag + // i; <--------- this part
  7. }

I can't figure out how to write the commented "i".
Help please! ^^
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 89
Reputation: brechtjah is an unknown quantity at this point 
Solved Threads: 9
brechtjah's Avatar
brechtjah brechtjah is offline Offline
Junior Poster in Training

Re: Square Root of -1

 
0
  #2
Nov 22nd, 2008
The square root of 1 is 1, this should lead to the square root of -1 being -1, however, this is imaginary. Taking the square root of a negative number doesn't work.

i = -1

Hope this helps
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 39
Reputation: ohnomis is an unknown quantity at this point 
Solved Threads: 0
ohnomis ohnomis is offline Offline
Light Poster

Re: Square Root of -1

 
0
  #3
Nov 22nd, 2008
So you're saying it should be the same as:

  1. int i = -1;
  2.  
  3. totalReal + totalImag + i;

Seems strange how the square root of -1 = -1...
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 89
Reputation: brechtjah is an unknown quantity at this point 
Solved Threads: 9
brechtjah's Avatar
brechtjah brechtjah is offline Offline
Junior Poster in Training

Re: Square Root of -1

 
0
  #4
Nov 22nd, 2008
Well,
the square root of 4 is 2 because 2*2 is 4. So the square root of x is a number which complies to this: y*y = x.
This works for every number that is positive, however, if you try to take the square root of -1, that means you want y

y*y = x
-1 * -1 = oops, no, it's not x!
it's 1 instead of -1. That means there is no square root of -1. We just say it is -1 because we want to be able to calculate complex numbers with it.

Maybe my previous explanation was a little short, I hope this helps feel free to ask questions
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 39
Reputation: ohnomis is an unknown quantity at this point 
Solved Threads: 0
ohnomis ohnomis is offline Offline
Light Poster

Re: Square Root of -1

 
0
  #5
Nov 22nd, 2008
Originally Posted by brechtjah View Post
Well,
the square root of 4 is 2 because 2*2 is 4. So the square root of x is a number which complies to this: y*y = x.
This works for every number that is positive, however, if you try to take the square root of -1, that means you want y

y*y = x
-1 * -1 = oops, no, it's not x!
it's 1 instead of -1. That means there is no square root of -1. We just say it is -1 because we want to be able to calculate complex numbers with it.

Maybe my previous explanation was a little short, I hope this helps feel free to ask questions
Sounds complicated lol, but I sorta get what you mean. =) So assuming a number * itself is = x
so if
2 * 2 = 4 // square root of 4 is equal to 2
-1 * -1 = doesn't work cuz all square roots are positive

so does this mean if i use
  1. totalReal + totalImaginary * 1 // or don't even bother with the *1 since it does nothing
should give me the correct answer?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 89
Reputation: brechtjah is an unknown quantity at this point 
Solved Threads: 9
brechtjah's Avatar
brechtjah brechtjah is offline Offline
Junior Poster in Training

Re: Square Root of -1

 
1
  #6
Nov 22nd, 2008
* -1
The square root of -1 is -1 we say. So it would just make your number negative.
Maybe this documentation is helpful for you too
http://en.wikipedia.org/wiki/Complex_number
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 39
Reputation: ohnomis is an unknown quantity at this point 
Solved Threads: 0
ohnomis ohnomis is offline Offline
Light Poster

Re: Square Root of -1

 
0
  #7
Nov 22nd, 2008
Originally Posted by brechtjah View Post
* -1
The square root of -1 is -1 we say. So it would just make your number negative.
Maybe this documentation is helpful for you too
http://en.wikipedia.org/wiki/Complex_number
I will take a look at wikipedia for a more mathematical answer. Its weird but my teacher's sample output has a different answer than mine even if it is negative lol. Guess theres something else wrong with my code then lol. Thank you for your help brechtjah.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 89
Reputation: brechtjah is an unknown quantity at this point 
Solved Threads: 9
brechtjah's Avatar
brechtjah brechtjah is offline Offline
Junior Poster in Training

Re: Square Root of -1

 
0
  #8
Nov 22nd, 2008
If you don't mind add your code and I'll try and take a look at it
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 53
Reputation: BeyondTheEye is an unknown quantity at this point 
Solved Threads: 9
BeyondTheEye BeyondTheEye is offline Offline
Junior Poster in Training

Re: Square Root of -1

 
0
  #9
Nov 22nd, 2008
sqrt(-1) = 1 * i
Where i can't be defined. You can't calculate it.

So if you were to return it as a result, it'd have to be a string like this:
"<Real part> + <Imaginary part> * i", in this case it would leave "0 + 1 * i", or "1i" in short.

Perhaps useful to add a String returning it this way to your class, or have a function do it.
Last edited by BeyondTheEye; Nov 22nd, 2008 at 8:59 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Square Root of -1

 
1
  #10
Nov 22nd, 2008
If you are making a complex class you should return a complex object, and it should not be the first operand. I mean given z and w two complex number, the instruction z + w should not modify z! Instead, it should be used in an expression like c = z + w , assigning the result of the sum to another complex number c.

The member you need in this class are just Real and Imaginary, that are two double variables holding real and imaginary part of the compex number itself. You don't need to bother about calculating "i" (in fact, you couldn't even if you needed to). You should just append "i" as a char next to the imaginary value when you print the complex number on screen.

Here are examples:
  1. Complex Complex::operator+(Complex op2) {
  2. Complex temp;
  3. temp.Real = Real + op2.Real;
  4. temp.Imaginary = Imaginary + op2.Imaginary;
  5. return temp;
  6. }

to overload the operator+, and
  1. std::ostream& operator<<(std::ostream& lhs, Complex& obj) {
  2. lhs << "(";
  3. if(obj.Real>=0) {
  4. lhs << "+" << obj.Real;
  5. }
  6. else {
  7. lhs << obj.Real;
  8. }
  9. if(obj.Imaginary>=0) {
  10. lhs << " +" << obj.Imaginary << "i";
  11. }
  12. else {
  13. lhs << " " << obj.Imaginary << "i";
  14. }
  15. lhs << ")";
  16. return lhs;
  17. }

for output purposes.

Last but not least: square root of -1 is not -1.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC