Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Complex Numbers

Status
Not open for further replies.

0x4B47

Programmer
Jun 22, 2003
60
US
Fellow coders,

We know that a complex number is a number that is represented as so:

ComplexNumber = realPart + imaginaryPart * squareroot of -1

How can we represent this number in a computer program?
Because the squareroot of -1 is an error.

Kunal.
 
Well you don't explicitly multiply everything by sqrt(-1), you just assume that is what happens at certain key points in the code. Like when you multiply two imaginary parts together, the result is negative (not positive).

Define a class, and some overloaded operators to deal with it.

Say something like
Code:
class complex {
  private:
    double real_part, imaginary_part;
  public:
    // constructors, destructor, assignment, copy
    complex & operator + ( complex & b );
};

Read this
Use this to determine how to combine the real/imaginary parts of two complex numbers to implement addition etc.

Then you can say things like
Code:
complex a, b, c;
c = a + b;

--
 
Your question is analogous to asking, "How can you represent Super Mario in a computer program when Little Mario + Mushroom results in an error?"

The key word is "represent."

The computer doesn't just magically contain the abstract notion of an integer. It gets represented as a series of bits that follow certain rules, thus modelling the concept of an integer.

Similarly, you can define a set of rules that operates on data, allowing it to represent a complex number. In other words, you can create a model of complex numbers.

It's just like how you can create a model of a string of letters, or of a list of floating point numbers, or of a keyboard, or of Super Mario.

The concept of Super Mario doesn't magically exist in the computer; you model him. Nothing magically exists in the computer; you model it.


Also, there's a complex number class in the Standard C++ Library. Once you understand the deal with modelling, I suggest you use that or another already-made complex number class instead of trying to roll your own.
 
chippermdw,

i know its modelling, but you didnt understand my question and just jumped into a frenzy of 'representation'. The reason why I asked what i did was from a 'performing a calculation' perspective, which Salem understood and answered for me quite simply and nicely.
I wasnt sure whether i had to explicitly calculate the square root of -1 to get a result for a Complex number.
 
I understood your question perfectly.


You just don't understand my answer, which is essentially a more abstract version of Salem's reply.

"Similarly, you can define a set of rules that operates on data, allowing it to represent a complex number. In other words, you can create a model of complex numbers."

is, if you stop to read it, essentially saying

"Write a class to represent complex numbers."


My "frenzy" was an attempt to get you to understand some basic computer science concepts and be able to apply them in the future to solve problems similar to the one you presented. I was unsuccessful, it seems.

You got an answer to your immediate problem, though. Enjoy your fish.
 
Let's invent wheels? Include STL <complex> header and enjoy:
Code:
#include <complex>
using namespace std;

typedef complex<double> Complex; // for example only...

Complex x, y;
...
Now we have complex arithmetics, assignments, i/o etc...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top