complex_numbers_test, a C code which demonstrates the features of using complex numbers in a C 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.
An unusual feature of the implementation of complex numbers in C is that a complex number can be defined by a statement like
z = a + b * I;where I is predefined; but I is actually implemented via a macro, and is not a numeric value. It needs to be multiplied by a number to be meaningful (this is how it determines the precision it will use, for instance). So you cannot set the imaginary unit by
a = I;Instead, you would have to write
a = 1 * I;
Another issue concerns the mistakes that can be made, which will evoke no warning from the compiler, but which will produce incorrect results. Here I am thinking, in particular, of the fact that if z is float complex or double complex, you should compute its exponential with the expression cexp(z); however, no compiler warning will occur if you use the expression exp(z); however, these two expressions are not equivalent, and will yield different results.
The information on this web page is distributed under the MIT license.
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.
blas1_z, a C code which contains basic linear algebra routines for vector-vector operations, using double precision complex arithmetic.
c8_complex_lib, a C code which defines a double precision complex variable or "C8" as a structure, and implements certain corresponding elementary functions.
c8lib, a C code which implements certain elementary functions for "C8" or double precision complex variables;