Chapter 18: Library Features Added by C99

The complex Class

The header <complex> defines the complex class, which represents complex numbers. It also defines a series of functions and operators that operate on objects of type complex.

The template specification for complex is shown here:

        template <class T> class complex

Here, T specifies the type used to store the components of a complex number. There are three predefined specializations of complex:

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

The complex class has the following constructors:

        complex(const T &real = T( ), const T = &imaginary = T( ));
        complex(const complex &ob);
        template <class T1> complex(const complex<T1> &ob);

The first constructs a complex object with a real component of real and an imaginary component of imaginary. These values default to zero if not specified. The second creates a copy of ob. The third creates a complex object from ob.

The following operations are defined for complex objects:

+

*

/

– =

+=

/=

*=

=

==

!=

 

The nonassignment operators are overloaded three ways: once for operations involving a complex object on the left and a scalar object on the right, again for operations involving a scalar on the left and a complex object on the right, and finally for operations involving two complex objects. For example, the following types of operations are allowed:

      complex_ob + scalar
      scalar + complex_ob
      complex_ob + complex_ob

Operations involving scalar quantities affect only the real component.

Two member functions are defined for complex: real( ) and imag( ). They are shown here:

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

The real( ) function returns the real component of the invoking object and imag( ) returns the imaginary component. The following functions are also defined for complex objects:

Function

Description

template <class T>
  T abs(const complex<T> &ob);

Returns the absolute value of ob

template <class T>
  T arg(const complex<T> &ob);

Returns the phase angle of ob

template <class T> complex<T>
  conj(const complex<T> &ob);

Returns the conjugate of ob

template <class T>
  complex<T> cos(const complex<T> &ob);

Returns the cosine of ob

template <class T>
  complex<T>
     cosh(const complex<T> &ob);

Returns the hyperbolic cosine of ob

template <class T>
  complex<T>
     exp(const complex<T> &ob);

Returns the eob

template <class T>
  T imag(const complex<T> &ob);

Returns the imaginary component of ob

template <class T>
  complex<T>
     log(const complex<T> &ob);

Returns the natural logarithm of ob

template <class T>
  complex<T>
     log10(const complex<T> &ob);

Returns the base 10 logarithm of ob

template <class T>
  T norm(const complex<T> &ob);

Returns the magnitude of ob squared

template <class T>
  complex<T>
     polar(const T &v, const T &theta=0);

Returns a complex number that has the magnitude specified by v and a phase angle of theta

template <class T>
  complex<T>
     pow(const complex<T> &b, int e);

Returns be

template <class T>
  complex<T>
     pow(const complex<T> &b,
              const T &e);

Returns be

template <class T>
  complex<T>
     pow(const complex<T> &b,
              const complex<T> &e);

Returns be

template <class T>
  complex<T>
     pow(const T &b,
              const complex<T> &e);

Returns be

template <class T>
  T real(const complex<T> &ob);

Returns the real component of ob

template <class T>
  complex<T> sin(const complex<T> &ob);

Returns the sine of ob

template <class T>
  complex<T>
     sinh(const complex<T> &ob);

Returns the hyperbolic sine of ob

template <class T>
  complex<T>
     sqrt(const complex<T> &ob);

Returns the square root of ob

template <class T>
  complex<T>
     tan(const complex<T> &ob);

Returns the tangent of ob

template <class T>
  complex<T>
     tanh(const complex<T> &ob);

Returns the hyperbolic tangent of ob




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net