Complex arithmetic in C++

Mathematical functions

A library of complex mathematical functions is provided. A complex function typically has a counterpart of the same name in the standard mathematical library. In this case the function name will be overloaded. That is, when called, the function to be invoked will be chosen based on the argument type. For example, log(1) will invoke the real log(), and log(complex(1)) will invoke the complex log(). In each case the integer 1 is converted to the real value 1.0.

These functions will produce a result for every possible argument. If it is not possible to produce a mathematically acceptable result, the function complex_error() will be called and some suitable value returned. In particular, the functions try to avoid actual overflow, calling complex_error() with an overflow message instead. The user can supply complex_error(). Otherwise a function that simply sets the integer errno is used. See ``Errors and error handling'' for details.

   complex conj(complex);
Conj(zz) returns the complex conjugate of zz.
   double norm(complex);
Norm(zz) returns the square of the magnitude of zz. It is faster than abs(zz), but more likely to cause an overflow error. It is intended for comparisons of magnitudes.
   double   pow(double, double);
   complex  pow(double, complex);
   complex  pow(complex, int);
   complex  pow(complex, double);
   complex  pow(complex, complex);

Pow(aa,bb) raises aa to the power of bb. For example, to calculate (1-i)**4:

   cout << pow( complex(1,-1), 4);
The output is (-4,0).
   double   log(double);
   complex  log(complex);

Log(zz) computes the natural logarithm of zz. Log(0), causes an error, and a huge value is returned.

   double   exp(double);
   complex  exp(complex);

Exp(zz) computes e**zz, e being 2.718281828...

   double   sqrt(double);
   complex  sqrt(complex);

Sqrt(zz) calculates the square root of zz.

The trigonometric functions available are:

   double   sin(double);
   complex  sin(complex);

double cos(double); complex cos(complex);

Hyperbolic functions are also available:

   double   sinh(double);
   complex  sinh(complex);

double cosh(double); complex cosh(complex);

Other trigonometric and hyperbolic functions, for example tan() and tanh(), can be written by the user using overloaded function names.

Next topic: Efficiency
Previous topic: Mixed mode arithmetic

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