Complex Numbers in C Programming

COMPLEX_NUMBERS is a Python program which demonstrates very briefly some of the features of using complex numbers in a C program.

The first issue is how to declare a complex variable, including the choice of 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.

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.

An unusual feature of the implementation of complex numbers in Python is that a complex number can be defined by a statement like

z = 1 + 2j;
where the string "2j" is interpreted as "2 times the imaginary unit". But this does NOT mean that j by itself means the imaginary unit. No, the imaginary unit is represented by "1j". So you cannot set the imaginary unit by
a = j;
Instead, you would have to write
a = 1j;


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


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

Related Data and Programs:

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

Source Code:

You can go up one level to the Python source codes.

Last revised on 10 December 2016.