Square Root of -1

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2008
Posts: 91
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
  #11
Nov 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,873
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Square Root of -1

 
1
  #12
Nov 22nd, 2008
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.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 40
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
  #13
Nov 22nd, 2008
Originally Posted by mrboolf View Post
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.
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
Reply With Quote Quick reply to this message  
Reply

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




Views: 1375 | Replies: 12
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC