943,763 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2500
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 22nd, 2008
0

Re: Square Root of -1

Yeah, my bad;
i² = -1
you can't calculate i but you can use it
Last edited by brechtjah; Nov 22nd, 2008 at 9:22 am.
Reputation Points: 26
Solved Threads: 9
Junior Poster in Training
brechtjah is offline Offline
92 posts
since Nov 2008
Nov 22nd, 2008
1

Re: Square Root of -1

Well, there are two common ways you can represent complex numbers. One is a+ib where i is the root of -1, which is NOT -1. This is called the Cartesian (or rectangular) coordinate system. The second way is to provide the equivalent hypotenuse of the a and b points (i.e. sqrt(a^2 + b^2)), and the angle between the origin (be it natural origin or a secondary), and the point a+ib (i.e. atan(b/a)). Might seem overly complicated, but it's good for multiplying and dividing imaginary numbers, though rectangular is better for addition and subtraction.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Nov 22nd, 2008
0

Re: Square Root of -1

Click to Expand / Collapse  Quote originally posted by mrboolf ...
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.
And the winner is mrboolf. I managed to solve the problem with your tips. Your answer is much more accurate in regards to what I needed to do. Lemme add to ur rep. ^^

Thanks to everyone else too for your help too, I'll remember to add to ur reps too. ;p

Now the only part left is to clean my code by declaring constants and using pass by reference where I can instead of passing by value. =P
Reputation Points: 46
Solved Threads: 0
Light Poster
ohnomis is offline Offline
40 posts
since Sep 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