| | |
Square Root of -1
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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.
•
•
Join Date: Sep 2008
Posts: 39
Reputation:
Solved Threads: 0
•
•
•
•
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)
Complex Complex::operator+(Complex op2) { Complex temp; temp.Real = Real + op2.Real; temp.Imaginary = Imaginary + op2.Imaginary; return temp; }
to overload the operator+, and
C++ Syntax (Toggle Plain Text)
std::ostream& operator<<(std::ostream& lhs, Complex& obj) { lhs << "("; if(obj.Real>=0) { lhs << "+" << obj.Real; } else { lhs << obj.Real; } if(obj.Imaginary>=0) { lhs << " +" << obj.Imaginary << "i"; } else { lhs << " " << obj.Imaginary << "i"; } lhs << ")"; return lhs; }
for output purposes.
Last but not least: square root of -1 is not -1.
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
![]() |
Similar Threads
- Square root program without sqrt or pwr (C++)
- C++ Square Root Problem (C++)
- simple problem with square root (C)
- help with square root finder (C++)
- Trying To Find The Square Root Of Each Number Of A Array (C++)
- Displaying Square root symbol in output (C)
Other Threads in the C++ Forum
- Previous Thread: Building a class in c++
- Next Thread: Genreic Double Linked List Using Templates
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






