# Mathematical

The mathematical libraries provide the common mathematical functions and some constants.

The module `RealMath` provides the constants and functions for the type `REAL`, while the module `LongMath` provides similar constants and functions for the type `LONGREAL`.

The module `ComplexMath` provides the constants and functions for the type `COMPLEX`, while the module `LongComplexMath` provides similar functions for the type `LONGCOMPLEX`.

- Modules RealMath and LongMath
- Modules ComplexMath and LongComplexMath

## Modules RealMath and LongMath

The semantics of the two modules is the same, except that where the module `RealMath` refers to the pervasive type `REAL`, the corresponding function in `LongMath` refers to the pervasive type `LONGREAL`.

NOTE:

The above statement is merely to avoid needless repetition of the semantics for `LongMath`.

The units used for angular quantities are radians.

- Constants - Useful constants
- sqrt - Calculate square root
- exp - Calculate exponent
- ln - Calculate natural logarithm
- sin - Calculate sine
- cos - Calculate cosine
- tan - Calculate tangent
- arcsin - Calculate arcsine
- arccos - Calculate arccosine
- arctan - Calculate arctangent
- power - Calculate power
- round - Round
- IsRMathException - Query exceptional state

### Constants - Useful constants

    CONST
      pi   = 3.1415926535897932384626433832795028841972;
      exp1 = 2.7182818284590452353602874713526624977572;

Modules: `RealMath`, `LongMath`

The constant `pi` provides an implementation-defined approximation to the mathematical constant . The constant exp1 provides an implementation-defined approximation to the mathematical constant e.

NOTE:

Due to the approximations involved, `sin(pi)` might not equal zero exactly; similarly, `exp1` might not equal `exp(1)` exactly.

### sqrt - Calculate square root

    PROCEDURE sqrt (x: REAL): REAL;
    PROCEDURE sqrt (x: LONGREAL): LONGREAL;

Modules: `RealMath`, `LongMath`

The function procedure `sqrt` returns an implementation-defined approximation to the positive signed square root of `x`. An exception is raised if `x` is negative.

### exp - Calculate exponent

    PROCEDURE exp (x: REAL): REAL;
    PROCEDURE exp (x: LONGREAL): LONGREAL;

Modules: `RealMath`, `LongMath`

The function procedure `exp` returns an implementation-defined approximation to the mathematical constant e raised to the power of `x`.

### ln - Calculate natural logarithm

    PROCEDURE ln (x: REAL): REAL;
    PROCEDURE ln (x: LONGREAL): LONGREAL;

Modules: `RealMath`, `LongMath`

The function procedure `ln` returns an implementation-defined approximation to the natural logarithm of `x`. An exception is raised if `x` is zero or negative.

### sin - Calculate sine

    PROCEDURE sin (x: REAL): REAL;
    PROCEDURE sin (x: LONGREAL): LONGREAL;

Modules: `RealMath`, `LongMath`

The function procedure `sin` returns an implementation-defined approximation to the sine of `x`.

### cos - Calculate cosine

    PROCEDURE cos (x: REAL): REAL;
    PROCEDURE cos (x: LONGREAL): LONGREAL;

Modules: `RealMath`, `LongMath`

The function procedure `cos` returns an implementation-defined approximation to the cosine of `x`.

### tan - Calculate tangent

    PROCEDURE tan (x: REAL): REAL;
    PROCEDURE tan (x: LONGREAL): LONGREAL;

Modules: `RealMath`, `LongMath`

The function procedure `tan` returns an implementation-defined approximation to the tangent of `x`. An exception is raised if `x` is an odd multiple of /2.

### arcsin - Calculate arcsine

    PROCEDURE arcsin (x: REAL): REAL;
    PROCEDURE arcsin (x: LONGREAL): LONGREAL;

Modules: `RealMath`, `LongMath`

The function procedure `arcsin` returns an implementation-defined approximation to the arcsine of `x`. An exception is raised if the absolute value of `x` is greater than one.

### arccos - Calculate arccosine

    PROCEDURE arccos (x: REAL): REAL;
    PROCEDURE arccos (x: LONGREAL): LONGREAL;

Modules: `RealMath`, `LongMath`

The function procedure `arccos` returns an implementation-defined approximation to the arccosine of `x`. An exception is raised if the absolute value of `x` is greater than one.

### arctan - Calculate arctangent

    PROCEDURE arctan (x: REAL): REAL;
    PROCEDURE arctan (x: LONGREAL): LONGREAL;

Modules: `RealMath`, `LongMath`

The function procedure `arctan` returns an implementation-defined approximation to the arctangent of `x`.

### power - Calculate power

    PROCEDURE power (base, exponent: REAL): REAL;
    PROCEDURE power (base, exponent: LONGREAL): LONGREAL;

Modules: `RealMath`, `LongMath`

The function procedure `power` returns an implementation-defined approximation to the result obtained by raising `base` to the power of `exponent`. An exception is raised if the value of base is zero or negative.

This function is mathematically equivalent to exp (y ln (x)) but may be computed differently.

### round - Round

    PROCEDURE round (x: REAL): INTEGER;
    PROCEDURE round (x: LONGREAL): INTEGER;

Modules: `RealMath`, `LongMath`

The function procedure `round` returns the nearest integer to the value of `x`. The result is an implementation-defined selection of the two possible values if the value of `x` is midway between two integer values. An exception occurs and may be raised if the mathematical result is not within the range of the type `INTEGER`.

### IsRMathException - Query exceptional state

    PROCEDURE IsRMathException (): BOOLEAN;
    PROCEDURE IsRMathException (): BOOLEAN;

Modules: `RealMath`, `LongMath`

If the calling coroutine is in the state of exceptional execution because of the raising of the `RealMath` exception, the function procedure `RealMath.IsRMathException` returns `TRUE`; otherwise the value is `FALSE`.

If the calling coroutine is in the state of exceptional execution because of the raising of the `LongMath` exception, the function procedure `LongMath.IsRMathException` returns `TRUE`; otherwise the value is `FALSE`.

## Modules ComplexMath and LongComplexMath

The semantics of the two modules are the same, except that where the module `ComplexMath` refers to the pervasive type `COMPLEX`, the corresponding function in `LongComplexMath` refers to the pervasive type `LONGCOMPLEX`.

NOTE:

The above statement is merely to avoid needless repetition of the semantics for `LongComplexMath`.

- Constants - Useful constants
- abs - Calculate modulus
- arg - Calculate argument
- conj - Calculate conjugate
- power - Calculate power
- sqrt - Calculate square root
- exp - Calculate exponent
- ln - Calculate natural logarithm
- sin - Calculate sine
- cos - Calculate cosine
- tan - Calculate tangent
- arcsin - Calculate arcsine
- arccos - Calculate arccosine
- arctan - Calculate arctangent
- polarToComplex - Convert from polar to complex
- scalarMult - Scalar Multiplication
- IsCMathException - Query exceptional state

### Constants - Useful constants

    CONST
      i = CMPLX (0.0, 1.0);
      one = CMPLX (1.0, 0.0);
      zero = CMPLX (0.0, 0.0);

Modules: `ComplexMath`, `LongComplexMath`

The constants `i`, `one`, and `zero` are the implementation-defined approximations to the specified values.

NOTE:

These constants are provided for convenience.

### abs - Calculate modulus

    PROCEDURE abs (z: COMPLEX): REAL;
    PROCEDURE abs (z: LONGCOMPLEX): LONGREAL;

Modules: `ComplexMath`, `LongComplexMath`

The function procedure `abs` returns an implementation-defined approximation to the modulus (otherwise known as the length, or absolute value) of `z`.

NOTE:

An overflow exception may be raised in this computation, even when the complex number is itself well defined.

### arg - Calculate argument

    PROCEDURE arg (z: COMPLEX): REAL;
    PROCEDURE arg (z: LONGCOMPLEX): LONGREAL;

Modules: `ComplexMath`, `LongComplexMath`

The function procedure `arg` returns an implementation-defined approximation to the angle that `z` subtends to the positive real axis in the complex plane. An exception is raised if the modulus of `z` is zero.

### conj - Calculate conjugate

    PROCEDURE conj (z: COMPLEX): COMPLEX;
    PROCEDURE conj (z: LONGCOMPLEX): LONGCOMPLEX;

Modules: `ComplexMath`, `LongComplexMath`

The function procedure `conj` returns an implementation-defined approximation to the complex conjugate of `z`.

### power - Calculate power

    PROCEDURE power (base: COMPLEX;
                     exponent: REAL
                    ): COMPLEX;
    PROCEDURE power (base: LONGCOMPLEX;
                     exponent: LONGREAL
                    ): LONGCOMPLEX;

Modules: `ComplexMath`, `LongComplexMath`

The function procedure `power` returns an implementation-defined approximation to the result obtained by raising `base` to the power of `exponent`.

### sqrt - Calculate square root

    PROCEDURE sqrt (z: COMPLEX): COMPLEX;
    PROCEDURE sqrt (z: LONGCOMPLEX): LONGCOMPLEX;

Modules: `ComplexMath`, `LongComplexMath`

The function procedure `sqrt` returns an implementation-defined approximation to the principal square root of `z`.

NOTE:

That is, the result is the complex number with an argument of half the value of the argument of `z`, and whose modulus has the value of the positive square root of the modulus of `z`.

### exp - Calculate exponent

    PROCEDURE exp (z: COMPLEX): COMPLEX;
    PROCEDURE exp (z: LONGCOMPLEX): LONGCOMPLEX;

Modules: `ComplexMath`, `LongComplexMath`

The function procedure `exp` returns an implementation-defined approximation to the mathematical constant e raised to the power of `z`.

### ln - Calculate natural logarithm

    PROCEDURE ln (z: COMPLEX): COMPLEX;
    PROCEDURE ln (z: LONGCOMPLEX): LONGCOMPLEX;

Modules: `ComplexMath`, `LongComplexMath`

The function procedure `ln` returns an implementation-defined approximation to the principal value of the natural logarithm of `z`.

### sin - Calculate sine

    PROCEDURE sin (z: COMPLEX): COMPLEX;
    PROCEDURE sin (z: LONGCOMPLEX): LONGCOMPLEX;

Modules: `ComplexMath`, `LongComplexMath`

The function procedure `sin` returns an implementation-defined approximation to the complex sine of `z`.

### cos - Calculate cosine

    PROCEDURE cos (z: COMPLEX): COMPLEX;
    PROCEDURE cos (z: LONGCOMPLEX): LONGCOMPLEX;

Modules: `ComplexMath`, `LongComplexMath`

The function procedure `cos` returns an implementation-defined approximation to the complex cosine of `z`.

### tan - Calculate tangent

    PROCEDURE tan (z: COMPLEX): COMPLEX;
    PROCEDURE tan (z: LONGCOMPLEX): LONGCOMPLEX;

Modules: `ComplexMath`, `LongComplexMath`

The function procedure `tan` returns an implementation-defined approximation to the complex tangent of `z`. An exception is raised if `z` is an odd multiple of /2.

### arcsin - Calculate arcsine

    PROCEDURE arcsin (z: LONGCOMPLEX): LONGCOMPLEX;
    PROCEDURE arcsin (z: COMPLEX): COMPLEX;

Modules: `ComplexMath`, `LongComplexMath`

The function procedure `arcsin` returns an implementation-defined approximation to the principal value of the complex arcsine of `z`.

### arccos - Calculate arccosine

    PROCEDURE arccos (z: COMPLEX): COMPLEX;
    PROCEDURE arccos (z: LONGCOMPLEX): LONGCOMPLEX;

Modules: `ComplexMath`, `LongComplexMath`

The function procedure `arccos` returns an implementation-defined approximation to the complex arccosine of `z`.

### arctan - Calculate arctangent

    PROCEDURE arctan (z: COMPLEX): COMPLEX;
    PROCEDURE arctan (z: LONGCOMPLEX): LONGCOMPLEX;

Modules: `ComplexMath`, `LongComplexMath`

The function procedure `arctan` returns an implementation-defined approximation to the complex arctangent of `z`.

### polarToComplex - Convert from polar to complex

    PROCEDURE polarToComplex (abs, arg: REAL): COMPLEX;
    PROCEDURE polarToComplex (abs, arg: LONGREAL): LONGCOMPLEX;

Modules: `ComplexMath`, `LongComplexMath`

The function procedure `polarToComplex` returns an implementation-defined approximation to the complex number that has a modulus given by `abs` and an argument given by `arg`.

### scalarMult - Scalar Multiplication

    PROCEDURE scalarMult (scalar: REAL;
                          z: COMPLEX
                         ): COMPLEX;
    PROCEDURE scalarMult (scalar: LONGREAL;
                          z: LONGCOMPLEX
                         ): LONGCOMPLEX;

Modules: `ComplexMath`, `LongComplexMath`

The function procedure `scalarMult` returns an implementation-defined approximation to the scalar product of the real value `scalar` with the complex value `z`.

### IsCMathException - Query exceptional state

    PROCEDURE IsCMathException (): BOOLEAN;
    PROCEDURE IsCMathException (): BOOLEAN;

Modules: `ComplexMath`, `LongComplexMath`

If the calling coroutine is in the state of exceptional execution because of the raising of a `ComplexMath` exception, the function procedure `ComplexMath.IsCMathException` returns `TRUE`; otherwise it returns `FALSE`.

If the calling coroutine is in the state of exceptional execution because of the raising of a `LongComplexMath` exception, the function procedure `LongComplexMath.IsCMathException` returns `TRUE`; otherwise it returns `FALSE`.