943,926 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2502
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 22nd, 2008
0

Square Root of -1

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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! ^^
Similar Threads
Reputation Points: 46
Solved Threads: 0
Light Poster
ohnomis is offline Offline
40 posts
since Sep 2008
Nov 22nd, 2008
0

Re: Square Root of -1

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
Reputation Points: 26
Solved Threads: 9
Junior Poster in Training
brechtjah is offline Offline
92 posts
since Nov 2008
Nov 22nd, 2008
0

Re: Square Root of -1

So you're saying it should be the same as:

C++ Syntax (Toggle Plain Text)
  1. int i = -1;
  2.  
  3. totalReal + totalImag + i;

Seems strange how the square root of -1 = -1...
Reputation Points: 46
Solved Threads: 0
Light Poster
ohnomis is offline Offline
40 posts
since Sep 2008
Nov 22nd, 2008
0

Re: Square Root of -1

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
Reputation Points: 26
Solved Threads: 9
Junior Poster in Training
brechtjah is offline Offline
92 posts
since Nov 2008
Nov 22nd, 2008
0

Re: Square Root of -1

Click to Expand / Collapse  Quote originally posted by brechtjah ...
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
C++ Syntax (Toggle Plain Text)
  1. totalReal + totalImaginary * 1 // or don't even bother with the *1 since it does nothing
should give me the correct answer?
Reputation Points: 46
Solved Threads: 0
Light Poster
ohnomis is offline Offline
40 posts
since Sep 2008
Nov 22nd, 2008
1

Re: Square Root of -1

* -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
Reputation Points: 26
Solved Threads: 9
Junior Poster in Training
brechtjah is offline Offline
92 posts
since Nov 2008
Nov 22nd, 2008
0

Re: Square Root of -1

Click to Expand / Collapse  Quote originally posted by brechtjah ...
* -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.
Reputation Points: 46
Solved Threads: 0
Light Poster
ohnomis is offline Offline
40 posts
since Sep 2008
Nov 22nd, 2008
0

Re: Square Root of -1

If you don't mind add your code and I'll try and take a look at it
Reputation Points: 26
Solved Threads: 9
Junior Poster in Training
brechtjah is offline Offline
92 posts
since Nov 2008
Nov 22nd, 2008
0

Re: Square Root of -1

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.
Reputation Points: 12
Solved Threads: 10
Junior Poster in Training
BeyondTheEye is offline Offline
63 posts
since Sep 2008
Nov 22nd, 2008
1

Re: Square Root of -1

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:
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 134
Solved Threads: 18
Junior Poster
mrboolf is offline Offline
182 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Building a class in c++
Next Thread in C++ Forum Timeline: Genreic Double Linked List Using Templates





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC