Top of document
©Copyright 1999 Rogue Wave Software

complex


     Complex Number Library 

Summary

C++ complex number library

Contents

Specializations

complex <float>
complex <double>
complex <long double>

Synopsis

#include <complex>
template <class T>
class complex ;

Description

complex<T> is a class that supports complex numbers. A complex number has a real part and an imaginary part. The complex class supports equality, comparison and basic arithmetic operations. In addition, mathematical functions such as exponentiation, logarithmic, power, and square root are also available.

Interface

template <class T>
class complex {
public:
   complex (T = 0 , T = 0);
   template <class X> complex
    (const complex<X>&);
   T real () const;
   T imag () const;
   template <class X>
    complex<T>& operator= (const complex<X>&);
   template <class X>
    complex<T>& operator+= (const complex<X>&);
   template <class X>
    complex<T>& operator-= (const complex<X>&);
   template <class X>
    complex<T>& operator*= (const complex<X>&);
   template <class X>
    complex<T>& operator/= (const complex<X>&);
};
// Non-member Operators
template<class T>
 complex<T> operator+ (const complex<T>&, const complex<T>&);
template<class T>
 complex<T> operator+ (const complex<T>&, T);
template<class T>
 complex<T> operator+ (T, const complex<T>&);
template<class T>
 complex<T> operator- (const complex<T>&, const complex<T>&);
template<class T>
 complex<T> operator- (const complex<T>&, T);
template<classT>
 complex<T> operator- (T, const complex<T>&);
template<class T>
 complex<T> operator* (const complex<T>&, const complex<T>&);
template<class T>
 complex<T> operator* (const complex<T>&, T);
template<class T>
 complex<T> operator* (T, const complex<T>&);
template<class T>
 complex<T> operator/ (const complex<T>&, const complex<T>&);
template<class T>
 complex<T> operator/ (const complex<T>&, T);
template<class T>
 complex<T> operator/ (T, const complex<T>&);
template<class T>
 complex<T> operator+ (const complex<T>&);
template<class T>
 complex<T> operator- (const complex<T>&);
template<class T>
 bool operator== (const complex<T>&, const complex<T>&);
template<class T>
 bool operator== (const complex<T>&, T);
template<class T>
 bool operator== (T, const complex<T>&);
template<class T>
 bool operator!= (const complex<T>&, const complex<T>&);
template<class T>
 bool operator!= (const complex<T>&, T);
template<class T>
 bool operator!= (T, const complex<T>&);
template <class X>
 istream& operator>> (istream&, complex<X>&);
template <class X>
 ostream& operator<< (ostream&, const complex<X>&);
// Values
template<class T> T real (const complex<T>&);
template<class T> T imag (const complex<T>&);
template<class T> T abs (const complex<T>&);
template<class T> T arg (const complex<T>&);
template<class T> T norm (const complex<T>&);
template<class T> complex<T> conj (const complex<T>&);
template<class T> complex<T> polar (T, T);
// Transcendentals
template<class T> complex<T> acos (const complex<T>&);
template<class T> complex<T> asin (const complex<T>&);
template<class T> complex<T> atan (const complex<T>&);
template<class T> complex<T> atan2 (const complex<T>&, 
                                    const complex<T>&);
template<class T> complex<T> atan2 (const complex<T>&, T);
template<class T> complex<T> atan2 (T, const complex<T>&);
template<class T> complex<T> cos (const complex<T>&);
template<class T> complex<T> cosh (const complex<T>&);
template<class T> complex<T> exp (const complex<T>&);
template<class T> complex<T> log (const complex<T>&);
template<class T> complex<T> log10 (const complex<T>&);
template<class T> complex<T> pow (const complex<T>&, int);
template<class T> complex<T> pow (const complex<T>&, T);
template<class T> complex<T> pow (const complex<T>&, 
                                  const complex<T>&);
template<class T> complex<T> pow (T, const complex<T>&);
template<class T> complex<T> sin (const complex<T>&);
template<class T> complex<T> sinh (const complex<T>&);
template<class T> complex<T> sqrt (const complex<T>&);
template<class T> complex<T> tan (const complex<T>&);
template<class T> complex<T> tanh (const complex<T>&);

Constructors

complex
(const T& re_arg = 0, const T& im_arg = 0);
template <class X> complex
(const complex<X>&);

Assignment Operators

template <class X>
complex<T> 
operator= (const complex<X>& c);
template <class X>
complex<T> 
operator+= (const complex<X>& c);
template <class X>
complex<T> 
operator-= (const complex<X>& c);
template <class X>
complex<T> 
operator*= (const complex<X>& c);
template <class X>
complex<T> 
operator/= (const complex<X>& c);

Member Functions

T 
imag() const;
T 
real() const;

Non-member Operators

template<class T> complex<T>
operator+ (const complex<T>& lhs,const complex<T>& rhs);
template<class T> complex<T>
operator+ (const complex<T>& lhs, T rhs);
template<class T> complex<T>
operator+ (T lhs, const complex<T>& rhs);
template<class T> complex<T>
operator- (const complex<T>& lhs,const complex<T>& rhs);
template<class T> complex<T>
operator- (const complex<T>& lhs, T rhs);
template<class T> complex<T>
operator- (T lhs, const complex<T>& rhs);
template<class T> complex<T>
operator*  (const complex<T>& lhs,const complex<T>& rhs);
template<class T> complex<T>
operator* (const complex<T>& lhs, T rhs);
template<class T> complex<T>
operator* (T lhs, const complex<T>& rhs);
template<class T> complex<T>
operator/ (const complex<T>& lhs,const complex<T>& rhs);
template<class T> complex<T>
operator/ (const complex<T>& lhs, T rhs);
template<class T> complex<T>
operator/ (T lhs, const complex<T>& rhs);
template<class T> complex<T>
operator+ (const complex<T>& rhs);
template<class T> complex<T>
operator- (const complex<T>& lhs);
template<class T> bool
operator== (const complex<T>& x, const complex<T>& y);
template<class T> bool
operator== (const complex<T>& x, T y);
template<class T> bool
operator== (T x, const complex<T>& y);
template<class T> bool
operator!= (const complex<T>& x, const complex<T>& y);
template<class T> bool
operator!= (const complex<T>& x, T y);
template<class T> bool
operator!= (T x, const complex<T>& y);
template <class X> istream& 
operator>> (istream& is, complex<X>& x);
template <class X> ostream& 
operator<< (ostream& os, const complex<X>& x);

Non-member Functions

template<class T> T 
abs(const complex<T>& c);
template<class T> complex<T> 
acos (const complex<T>& c);
template<class T> T 
arg (const complex<T>& c);
template<class T> complex<T> 
asin (const complex<T>& c);
template<class T> complex<T> 
atan (const complex<T>& c);
template<class T> complex<T>
atan2 (T a, const complex<T>& b);
template<class T> complex<T>
atan2 (const complex<T>& a, T b);
template<class T> complex<T>
atan2 (const complex<T>& a, const complex<T>& b);
template<class T> complex<T> 
conj (const complex<T>& c);
template<class T> complex<T> 
cos (const complex<T>& c);
template<class T> complex<T> 
cosh (const complex<T>& c);
template<class T> complex<T> 
exp (const complex<T>& x);
template<class T> T 
imag (const complex<T>& c) const;
template<class T> complex<T> 
log (const complex<T>& x);
template<class T> complex<T> 
log10 (const complex<T>& x);
template<class T> T 
norm (const complex<T>& c);
template<class T> complex<T>
polar (const T& m, const T& a);
template<class T> complex<T>
pow (const complex<T>& x, int y);
template<class T> complex<T>
pow (const complex<T>& x, T y);
template<class T> complex<T>
pow (const complex<T>& x, const complex<T>& y);
template<class T> complex<T>
pow (T x, const complex<T>& y);
template<class T> T 
real (const complex<T>& c);
template<class T> complex<T> 
sin (const complex<T>& c);
template<class T> complex<T> 
sinh (const complex<T>& c);
template<class T> complex<T> 
sqrt (const complex<T>& x);
template<class T> complex<T> 
tan (const complex<T>& x);
template<class T> complex<T> 
tanh (const complex<T>& x);

Example

//
// complex.cpp
//
 #include <complex>
 #include <iostream.h>
 int main() 
 {
   complex<double> a(1.2, 3.4);
   complex<double> b(-9.8, -7.6);
   a += b;
   a /= sin(b) * cos(a);
   b *= log(a) + pow(b, a);
   cout << "a = " << a << ", b = " << b << endl;
   return 0;
 }
Output : 
a = (1.42804e-06,-0.0002873), b = (58.2199,69.7354)

Warnings

On compilers that don't support member function templates, the arithmetic operators will not work on any arbitrary type. (They will work only on float, double and long doubles.) You also will only be able to perform binary arithmetic on types that are the same.

Compilers that don't support non-converting constructors will permit unsafe downcasts (i.e., long double to double, double to float, long double to float).


Top of document