Complex Numbers in FORTRAN90 Programming

**complex_numbers_test**,
a FORTRAN90 code which
demonstrates some of the features of using complex
numbers in a FORTRAN90 code.

The first issue is how to declare a complex variable, including the choice of single precision or double precision, whether the variable is a scalar, vector, or array, and whether the variable is initialized with a value, or assigned one.

A second issue concerns the question of how a complex variable is to be printed out.

Another issue concerns how a complex variable is to operated on by the arithmetic operators of addition, subtraction, multiplication, division, and exponentiation.

The language also provides a number of intrinsic functions that can be applied to a complex variable. The names of these functions can sometimes be easy to forget. Moreover, it is occasionally true that there may be a selection of functions with similar names (say, "exp", "cexp" and "dcexp") which may or may not produce the desired results.

Another issue concerns the details of double precision calculation. Even a single accidental use of a single precision function name in a double precision computation can result in the loss of half the digits of accuracy. Thus, it sometimes really matters whether you use "cmplx" or "dcmplx" to assign values to a double precision complex variable.

A peculiar feature of FORTRAN90 is that the generic **cmplx()** function
will produce a result whose real and imaginary parts are single precision,
no matter what the type of the input arguments or the target output value.
The only ways to avoid this are to add the **kind=** qualifier, as in

a = cmplx ( b, c, kind = 8 )or to use the dcmplx() function specific to double precision:

a = dcmplx ( b, c )

The computer code and data files described and made available on this web page are distributed under the MIT license

**complex_numbers_test** is available in
a C version and
a C++ version and
a FORTRAN90 version and
a MATLAB version and
a Python version.

C4LIB, a FORTRAN90 code which implements certain elementary functions for "C4" or single precision complex variables;

C8LIB, a FORTRAN90 code which implements certain elementary functions for "C8" or double precision complex variables;

F90_INTRINSICS, FORTRAN90 codes which include some examples of the use of intrinsic functions included with the FORTRAN90 standard language.

GFORTRAN_INTRINSICS, FORTRAN90 codes which include some examples of the use of intrinsic functions included with the GFORTRAN compiler.

- complex_numbers_test.f90, the source code.
- complex_numbers_test.sh, runs all the tests.
- complex_numbers_test.txt, the output file;