complex_numbers_test
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 )

Licensing:

The information on this web page is distributed under the MIT license.

Languages:

complex_numbers_test is available in a C version and a C++ version and a Fortran77 version and a Fortran90 version and a MATLAB version and an Octave version and a Python version.

Related Data and Programs:

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

f90_intrinsics_test, a Fortran90 code hich demonstrates intrinsic functions included with the Fortran90 standard language.

gfortran_intrinsics_test, a Fortran90 code which demonstrates intrinsic functions included with the GFortran compiler.

Source Code:


Last revised on 05 November 2010.