C complex number usage

Jerry Feldman gaf at blu.org
Sun Jan 6 17:34:09 EST 2008


On Sun, 06 Jan 2008 16:47:23 -0500
Bruce Labitt <bruce.labitt at verizon.net> wrote:

> Nope, I'm using Dev-C++ which is based on the MinGW compiler.  (gcc)
> 
> I'm just a bit frustrated that I can't even form a single complex number 
> and print it out. :(
> 
> I've got a simpler scrap of code that doesn't work either. ;)
> compiled with c++ compiler...
> 
> #include <cstdlib>
> #include <iostream>
> #include <complex>
> using namespace std;
> 
> int main(int argc, char *argv[])
> {
>     double a, b;
>     complex<double> c;
>    
>     a = 1.0;    // a is the real part
>     b = 1.0;    // b is the imaginary part
>     c = (a,b);  // form the complex number
>     cout << c;
>      
>     system("PAUSE");
>     return EXIT_SUCCESS;
> }
> The console *should* return with (1,1), but returns with (1,0). :(  Any 
> ideas?
>
There is a bug in your code:
c = (a,b);  // form the complex number
This is not initialzing c. It is assigning a to c.
The correction is:
#include <cstdlib>
#include <iostream>
#include <complex>
using namespace std;

int main(int argc, char *argv[])
{
    double a, b;
   
    a = 1.0;    // a is the real part
    b = 1.0;    // b is the imaginary part
    complex<double> c(a, b);
    //    c = (a,b);  // form the complex number
    cout << c << "\n";
     
    return EXIT_SUCCESS;
}
gaf at gaf:~/src> ./labitt
(1,1)



-- 
--
Jerry Feldman <gaf at blu.org>
Boston Linux and Unix
PGP key id: 537C5846
PGP Key fingerprint: 3D1B 8377 A3C0 A5F2 ECBB  CA3B 4607 4319 537C 5846
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 194 bytes
Desc: not available
Url : http://mail.gnhlug.org/mailman/private/gnhlug-discuss/attachments/20080106/b5e81445/attachment.bin 


More information about the gnhlug-discuss mailing list