United States    
COMPAQ STORE | PRODUCTS |
SERVICES | SUPPORT | CONTACT US | SEARCH
C++
Compaq C++

Compaq C++
Class Library Reference Manual


Previous Contents Index


Chapter 2
complex Package

The complex package provides ways to perform arithmetical operations, such as initialization, assignment, input, and output, on complex values (that is, numbers with a real part and an imaginary part). Additionally, this package supports operations that are unique to complex values, such as principal argument operations, conjugate operations, and conversions to and from polar coordinates.

With the c_exception class and its c_exception function, the complex package also provides a mechanism for reporting and handling complex arithmetical errors.


Global Declarations

These declarations are used by the complex package but they are not members of the complex class.

Header

#include <complex.hxx>

Alternative Header

#include <complex.h>


Declarations


typedef int (*cxxl_p_complex_error_t)(c_exception &error_information); 
static const complex_zero (0, 0); 
cxxl_p_complex_error_t set_complex_error(cxxl_p_complex_error_t 
p_complex_error); 


Type

cxxl_p_complex_error_t

Is the type of the complex_error function.

Data

static const complex_zero (0, 0)

Is a constant object of type complex and value 0 created in each module that uses the complex package.

Function

cxxl_p_complex_error_t set_complex_error (cxxl_p_complex_error_t p_complex_error)

Causes the function pointed to by p_complex_error to be called instead of the complex_error function on subsequent complex arithmetical errors. If set_complex_error() previously has not been called, then it returns 0; otherwise, it returns the address of the last function passed to it.

See the Other Function section of c_exception class for a description of the error-handling function.


complex class

Provides a representation of, and lets you perform operations on, complex values.

Header

#include <complex.hxx>

Alternative Header

#include <complex.h>


Declaration


class complex 
{ 
    friend complex   polar(double, double = 0); 
    friend double    abs(const complex &); 
    friend double    norm(const complex &); 
    friend double    arg(const complex &); 
    friend double    arg1(const complex &); 
    friend complex   conj(const complex &); 
    friend complex   sin(const complex &); 
    friend complex   sinh(const complex &); // c_exception OVERFLOW 
    friend complex   cos(const complex &); 
    friend complex   cosh(const complex &); // c_exception OVERFLOW 
    friend complex   tan(const complex &); 
    friend complex   tanh(const complex &); 
    friend double    imag(const complex &); 
    friend double    real(const complex &); 
    friend complex   log(const complex &); // c_exception SING 
    // c_exception OVERFLOW UNDERFLOW 
    friend complex   exp(const complex &); 
    friend complex   pow(double, const complex &); 
    friend complex   pow(const complex &, int); 
    friend complex   pow(const complex &, double); 
    friend complex   pow(const complex &, const complex &); 
    friend complex   sqrt(const complex &); 
    friend complex   sqr(const complex &); 
    friend complex   operator-(const complex &); 
    friend complex   operator+(const complex &, const complex &); 
    friend complex   operator-(const complex &, const complex &); 
    friend complex   operator*(const complex &, const complex &); 
    friend complex   operator/(const complex &, const complex &); 
    friend int       operator==(const complex &, const complex &); 
    friend int       operator!=(const complex &, const complex &); 
    friend ostream   &operator<<(ostream &, const complex &); 
    friend istream   &operator>>(istream &, complex &); 
 
public: 
 
                     complex(double, double = 0); 
                     complex(); 
 
    inline complex   &operator-=(const complex &); 
    inline complex   &operator+=(const complex &); 
    complex          &operator*=(const complex &); 
    complex          &operator/=(const complex &); 
}; 


Description

This class contains methods to perform complex value operations. These include arithmetical, assignment, and comparison operators for complex values; Cartesian and polar coordinates; mixed-mode arithmetic; and mathematical functions for complex values equivalent to standard mathematical functions.

Exception Handling

When a complex arithmetical error is detected, a c_exception object is created with one of the following values for type :
Value Error Description
OVERFLOW Value too large to be represented
SING Function undefined for argument
UNDERFLOW Value too small to be represented

This object is then passed to the complex_error function (see the c_exception class).


Constructors and Destructors

complex()

Constructs and initializes a complex value to 0.

complex(double x, double y = 0)

Constructs and initializes a complex value from Cartesian coordinates.

Overloaded Operators

complex operator + (const complex &z1, const complex &z2)

Returns the arithmetical sum of the complex values z1 and z2.

complex operator -- (const complex &z1)

Returns the arithmetical negation of a complex value.

complex operator -- (const complex &z1, const complex &z2)

Returns the arithmetical difference of complex values. That is, z2 is subtracted from z1.

complex operator * (const complex &z1, const complex &z2)

Returns the arithmetical product of the complex values z1 and z2.

complex operator / (const complex &z1, const complex &z2)

Returns the arithmetical quotient of complex values. That is, z1 is divided by z2.

inline complex &operator += (const complex &z1)

Assigns the arithmetical sum of complex values to the complex object on the left side of an equation. That is, z1+=z2 is equivalent to z1=z1+z2.

inline complex &operator --= (const complex &z1)

Assigns the arithmetical difference of two complex numbers to the complex object on the left side of an equation. That is, z1--=z2 is equivalent to z1=z1--z2.

complex &operator *= (const complex &z2)

Assigns the arithmetical product of two complex numbers to the complex object on the left side of an equation. That is, z1*=z2 is equivalent to z1=z1*z2.

complex &operator /= (const complex &z2)

Assigns the arithmetical quotient of two complex numbers to the complex object on the left side of an equation. That is, z1/=z2 is equivalent to z1=z1/z2.

ostream &operator << (ostream &s, const complex &z1)

Sends a complex value to an output stream in the format (real,imag) . It returns the left argument s.

istream &operator >> (istream &s, complex &z1)

Takes a complex value from an input stream. The numbers may be of the forms (real,imag) or (real) , where real and imag are what the iostream library accepts for parameters of type double . The iostream library also determines how to handle white space. This operator returns the left argument s. The following input format omissions will cause an error:

int operator == (const complex &z1, const complex &z2)

Compares two complex values and returns a nonzero value if the two numbers are equal; otherwise, it returns 0.

int operator != (const complex &z1, const complex &z2)

Compares two complex values and returns a nonzero value if the two numbers are not equal; otherwise, it returns 0.

Other Functions

double abs(const complex &z1)

Returns the absolute value (magnitude) of a complex value.

double arg(const complex &z1)

Returns the angle, in radians, of a complex value. The result is normalized such that it is greater than or equal to 0, and less than 2*Pi sign .

double arg1(const complex &z1)

Returns the principal value of the angle, in radians, of a complex value. The result is normalized such that it is greater than -Pi sign , and less than or equal to Pi sign .

complex conj(const complex &z1)

Returns the conjugate of a complex value; that is, if the number is
(real, imag) , then the result is (real, -imag) .

complex cos(const complex &z1)

Returns the cosine of a complex value.

complex cosh(const complex &z1)

Returns the hyperbolic cosine of a complex value. The value of real(z1) must be small enough so that exp(real(z1)) does not overflow; otherwise, the function creates a c_exception object and invokes the complex_error function.

complex exp(const complex &z1)

Returns the value of e (2.71828...) raised to the power of a complex value. The conditions described for cosh() must be met; otherwise, it creates a c_exception object and invokes the complex_error function.

double imag(const complex &z1)

Returns the imaginary part of a complex value.

complex log(const complex &z1)

Returns the natural logarithm (base e, 2.71828...) of a complex value. The conditions described for cosh() must be met; otherwise, it creates a c_exception object and invokes the complex_error function.

double norm(const complex &z1)

Returns the square of the absolute value (magnitude) of a complex value.

complex polar(double rho, double theta = 0)

Creates a complex value given a pair of polar coordinates (magnitude rho and angle theta, in radians).

complex pow(const complex &z1, int i2)

Returns the value of z1 raised to the power of i2.

complex pow(const complex &z1, double x2)

Returns the value of z1 raised to the power of x2.

complex pow(double z1, const complex &z2)

Returns the value of z1 raised to the power of z2.

complex pow(const complex &z1, const complex &z2)

Returns the value of z1 raised to the power of z2.

double real(const complex &z1)

Returns the real part of a complex value.

complex sin(const complex &z1)

Returns the sine of a complex value.

complex sinh(const complex &z1)

Returns the hyperbolic sine of a complex value. The conditions described for cosh() must be met; otherwise, it creates a c_exception object and invokes the complex_error function.

complex sqr(const complex &z1)

Returns the square of a complex value.

complex sqrt(const complex &z1)

Returns the square root of a complex value.

complex tan(const complex &z1)

Returns the tangent of a complex value.

complex tanh(const complex &z1)

Returns the hyperbolic tangent of a complex value. The conditions described for cosh() must be met; otherwise, it creates a c_exception object and invokes the complex_error function.

Examples

#1

complex zz(3,-5); 
      

Declares zz to be a complex object and initializes it to the value of real part 3 and imaginary part -5 .

#2

complex myarray[30]; 
      

Declares an array of 30 complex objects, all initialized to (0,0).

#3

complex zz; 
while (!(cin >> zz).eof()) 
    cout << zz << endl; 
      

Reads a stream of complex values [for example, ( 3.400000,5.000000 )] and writes them in the default format [for example, ( 3.4, 5 )].

#4

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

Prints out 3.4 as the real part of a complex object and 5 as the imaginary part. The result is 3.4+5*i .


c_exception class

Contains information on a complex arithmetical exception.

Header

#include <complex.hxx>

Alternative Header

#include <complex.h>


Declaration


class c_exception 
{ 
    friend complex   exp(const complex &); 
    friend complex   sinh(const complex &); 
    friend complex   cosh(const complex &); 
    friend complex   log(const complex &); 
    friend int       complex_error(c_exception &); 
 
public: 
    int              type; 
    char             *name; 
    complex          arg1; 
    complex          arg2; 
    complex          retval; 
 
public: 
    c_exception(char *, const complex &, const 
    complex & = complex_zero); 
}; 


Description

Objects of this class handle exceptions for complex arithmetic. This includes information on functions, parameters, error types, and default return values.


Data Members

complex arg1

Is the left argument of the function that incurred the error.

complex arg2

Is the right argument of the function that incurred the error.

char *name

Is the name of the function that incurred the error.

complex retval

Is the value to be returned by the function that incurred the error. You may use the complex_error(c_exception &) function to change this value.

int type

Is one of these kinds of error: SING , OVERFLOW , or UNDERFLOW .

Constructor

c_exception(char *function_name, const complex &function_arg1, const complex &function_arg2 = complex_zero)

Constructs a complex arithmetical exception object, with reference to the name and arguments of the function that incurred the error.

Other Function

int complex_error (c_exception &error_information)

Is the default error-handling function that is called by certain complex arithmetical functions in this package (namely, cosh , exp , log , and sinh ) when those functions detect an arithmetical error. You may replace this function with your own function that takes an identical parameter list and returns a value as specified in the following table:
Return Value from Error-handling Function Action Taken by Complex Arithmetical Function
0 Set the global value errno ; if the error type is SING , print an error message.
non 0 Do not set errno ; do not print an error message.

To substitute your own error-handling function, pass a pointer to your function to the set_complex_error function. (See the Function section under Global Declarations for the complex package.)

The complex arithmetical functions that invoke the error handling function always return the value specified in error_information .retval . Your error-handling function may set this value.


Previous Next Contents Index
  

1.800.AT.COMPAQ

privacy and legal statement