View Full Version : Complex numbers in C++

February 4th, 2009, 01:38 AM
I'm having a ridiculous amount of trouble trying to work with complex numbers in C++... I can't even successfully declare the damn things.
I've tried including <complex> and <complex.h>, because from what I can gather there are subtle differences between the two, but with no luck.

I've spent hours (no joke) trying to work out how to do this, and I rightly feel very stupid at the moment.

Could somebody please just write out the code to declare a complex number for me and save me a lot of trouble?



February 4th, 2009, 02:35 AM
The <complex> is templated, complex.h is not. Here is my use of <complex>. This compiles with g++.

using namespace std;

int main(void)
complex <double> c1;

February 4th, 2009, 02:45 AM
I cannot get the complex.h to work at all. There are lots of examples out there but none of them compile for me. Not that it matters since the <complex> works, but this was bothering me...

February 4th, 2009, 02:46 AM
this works for me

#include <iostream>
#include <complex>
using namespace std;
int main(){
complex<float> numone (3,5);
complex<float> numtwo (7,11);
complex<float> res1 = numone+numtwo;
complex<float> res2 = numone*numtwo;

return 0;

February 5th, 2009, 01:54 AM
Thank you to you both, although only monkeyking's method works for me.
In retrospect, for the fairly simple purposes for which I need this functionality, I could have just written my own code!
I failed at finding a simple, working library for matrix manipulation, so I just ended up writing my own code to multiply small matrices, and it's proved much less of a headache.
Thanks :)