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.
The complex package makes use of other packages. To ensure that all
necessary libraries are linked into the executable program image,
certain switches need to be provided to the linker through the
cxx command. For detailed information on the switches
to use, see Using DEC C++ for
Digital UNIX Systems.
These declarations are used by the complex package but they are not members of the complex class.
#include <complex.hxx>
#include <complex.h>
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);
complex_error function. complex and value 0
created in each module that uses the complex package. 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.
Provides a representation of, and lets you perform operations on, complex values.
#include <complex.hxx>
#include <complex.h>
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
friend complex exp(const complex &); // c_exception OVERFLOW UNDERFLOW
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 &);
};
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).
(real,imag) . It returns the left argument s.
(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:
(real, imag) , then the result is (real, -
imag) . 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. cosh()
must be met; otherwise, it creates a c_exception object
and invokes the complex_error function. cosh() must be met;
otherwise, it creates a c_exception object and invokes
the complex_error function. cosh() must be met; otherwise, it creates
a c_exception object and invokes the complex_
error function. cosh() must be met; otherwise,
it creates a c_exception object and invokes the
complex_error function. 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 .
complex myarray[30];Declares an array of 30 complex objects, all initialized to (0,0).
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 )].
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 . Contains information on a complex arithmetical exception.
#include <complex.hxx>
#include <complex.h>
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);
};
complex_error(c_exception &)
function to change this value. SING ,
OVERFLOW , or UNDERFLOW . 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.