| | |
Square Root of -1
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
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: 40
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
Views: 1375 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library lines linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return simple sort spoonfeeding stream string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






