DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Complex arithmetic in C++

Cartesian and polar coordinates

The functions real() and imag() return the real and imaginary parts of a complex number, respectively. This can, for example, be used to create differently formatted output of a complex:

   complex cc = complex(3.4,5);
   cout << real(cc) << "+" << imag(cc) << "*i";
will print 3.4+5*i.

The function polar() creates a complex given a pair of polar coordinates (magnitude, angle). The functions arg() and abs() both take a complex argument and return the angle and magnitude (modulus), respectively. For example:

   complex cc = polar(SQRT_2,PI/4);  // also known as complex(1,1)
   double magn = abs(cc);            // magn = sqrt(2)
   double angl = arg(cc);            // angl = PI/4
   cout << "(m=" << magn << ", a=" << angl << ")";

If input and output functions for the polar representation of complex numbers are needed they can easily be written by the user.


Next topic: Arithmetic operators
Previous topic: input and output

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004