MC logo

Problem 3.4: Complex Type for Algol


CS 404: Solutions and Examples

Design a complex type for Algol. Discuss operators, relations, coercions, declarations, etc.

The question indicates which areas need to be considered, though I'll add the problem of how to construct a complex value. You can think of it as part of operators, coercions, declarations, or etc., as you like.

First off, the complex number would be represented as a real and imaginary part, each with the same precision as a real number.

Declaration:
Declaring variables is accomplished simply by adding a keyword complex to be used in declarations.

Construction:
Once we have variables to hold complex values, we need to get some complex values. The simplest solution might be to permit a notation like (<x>, <y>) denoting a complex number formed from two reals or integers, <x> and <y>. The complex number has the value <x>+<y>i, where i is the imaginary. (<x>, <y>) is not already a legal expression, so the compiler will be able to recognize it as something special. It might, however, leave programmers a little confused, as func(a, b) calls func with two arguments, while func((a, b)) calls it with a single complex argument.

To really get fancy, we could also allow something like (<r>@ <t>) to specify a complex number in polar co-ordinates. Of course, then you'd have to decide if <t> is in radians or degrees. Probably not worth it.

An alternative would be to specify a symbol to represent the imaginary. Using Algol's multilevel notation, we could just call it i. It would simply represent the complex number 0+1i. Then, complex numbers could be formed using the regular arithmetic operations, plus coercion from real to complex (see below). For instance, 5.1 + 3.2*i. This is perhaps a more elegant solution, but it leaves the problem of how to write i to the implementor. He might be lazy and make i a reserved word, which is a terrible idea. Perhaps #i would be tolerable. There doesn't seem to be any wonderful choice for the notation.

Operators:
The basic arithmetic operators +, -, * and / are defined for complex numbers, and yield a complex result. FORTRAN also has an exponent operator, which will work the same way.

Coercion:
Automatic conversion from integer or real to complex should be allowed, since these can be performed without loss of information. (Well, mostly. A conversion from integer could lose precision, depending on the relative sizes.) No other automatic coercions seem safe. Particularly, any conversion to a real or integer from complex would generally lose information.

We would also need some way to extract the real and imaginary parts of a complex number as real numbers. The simplest thing is probably a notation like Re(<x>) or Im(<x>) for the real and imaginary parts of <x>, respectively.

Relations:
Comparisons = and <> make sense for complex; the other standard relations, <, <=, > and >= do not. I would leave it at that. The programmer can use Re and Im to build any desired ordering.

Etcetera:
ALGOL does not specify I/O. We should note that that leaves a considerable amount of design work to the implementor.

It might also be desirable to specify a list of "standard" library functions which would work with complex numbers, such as absolute value, or various kinds of orderings.

>>