Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

cplxerr Error handling functions


This section describes the error handling function used for complex math in C++.


#include <complex.h>

class c_exception

{

int
char
complex
complex
complex

type;
*name;
arg1;
arg2;
retval;

public:

c_exception(char *n, const complex& a1, const complex& a2 = complex_zero);

friend int complex_error(c_exception&);

friend complex exp(complex);
friend complex sinh(complex);
friend complex cosh(complex);
friend complex log(complex);

};


c_exception(char *n, const complex& a1, const complex& a2 = complex_zero);

friend int              complex_error(c_exception&);

friend complex exp(complex);
friend complex sinh(complex);
friend complex cosh(complex);
friend complex log(complex);

};


int i = complex_error(c_exception & x)

The error handling function complex_error is called if an error occurs for one of the following four functions:

friend complex exp(complex)
friend complex sinh(complex)
friend complex cosh(complex)
friend complex log(complex)


Users may define their own routines for handling errors, by defining a function named complex_error in their programs. complex_error must be of the form described above.

In the class c_exception, the element type is an integer describing the type of error that has occurred, from the following list of constants (defined in the header file <complex.h>):

SING
OVERFLOW
UNDERFLOW

argument singularity
overflow range error
underflow range error

The element name points to a string containing the name of the function that producedthe error. The variables arg1 and arg2 are the arguments with which the function wasinvoked. retval is set to the default value that is returned by the function unless theuser’s complex_error sets it to a different value.

If the user’s complex_error function returns a non-zero value, no error message isprinted, and errno is not set.

If the user does not supply a function called complex_error, the default error handlingroutines described under the heading "RETURN VALUES" with the respective functionsare invoked upon error. Default error handling is also summarized in the table below. Inevery case, errno is set to EDOM or ERANGE and the program continues.

The following abbreviations are used in the table below:

M
(H, 0)
(±H, ±H)
(0, 0)

Message is printed (EDOM error).
(HUGE, 0) is returned.
(±HUGE, ±HUGE) is returned.
(0, 0) is returned.

DEFAULT ERROR HANDLING ROUTINE           


Types of Errors

type

SING

OVERFLOW

UNDERFLOW

errno

EDOM

ERANGE

ERANGE

EXP:

real too large or small

imag too large



(±H, ±H)

(0, 0)

 

(0,0)

LOG:

arg = (0, 0)

 

M, (H, 0)

 

-

 

-

SINH:

real too large

imag too large



(±H, ±H)

(0, 0)


COSH:

real too large

imag too large


 

(±H, ±H)

(0, 0)


EXAMPLE

The following program declares a complex number using the default constructor, which
gives (0.0, 0.0), and then calls the log() function with (0.0, 0.0). This produces an error
since log(0.0, 0.0) is undefined. The complex_error() function is called to handle the
error.

#include <iostream.h>
#include <complex.h>
#include <stdlib.h>
int complex_error(c_exception & p)
{
  cerr << "Error when processing ";
  cerr << p.name << " ( " << p.arg1 << " )\n";
  exit (1);
  return 0; /* NOT REACHED */
}
main()
{
  complex c;
  c = log (c);
  return 0;
}

The result of executing the program is:

Error when processing log ( ( 0, 0) )
%  CCM0998 CPU time used: 0.0005 seconds
%  CCM0999 exit 1

SEE ALSO


cplxcartpol, cplxexp, cplxops, cplxtrig