Computers and Complex Numbers

Today the treatment of real numbers by computers is precisely regulated by the IEEE-754 standard. Everyone religiously obeys this standard without questioning its utility or cost.

But complex numbers are a little different. IEEE-754 does not mention them, and this leads to different approaches. Here some of the resulting surprises are discussed.

Four main approaches are considered here:

  1. Fortran: no rules
  2. C99 and cx_limited_range
  3. C99 Annex G
  4. Gfortran

For completeness, C++ and sanity are also considered.

In some cases the result of a seemingly simple calculation is not precisely defined. In some cases it is, but differently by different languages.

The Problems

All computer languages agree that complex numbers are stored in Cartesian form, as real and imaginary components. It is not entirely obvious that this is more correct than using the modulus-argument form of reiθ, but it does simplify addition.

Having stored numbers thus, and using the IEEE types for the real and imaginary components, one is left with the problem of how to treat the "special" IEEE values of signed zeros, infinities and NaNs in the individual components.

There is also a significant problem when performing division and absolute value operations. Whereas

|reiθ|=r,

in Cartesian form

|a+ib|=√(a²+b²)

But for a huge range of numbers this will result in trying to take the square root of zero, or of infinity, when the correct answer is finite and non-zero.

Division has a similar problem when the divisor has a modulus which cannot be squared without overflowing or underflowing.

This is already sufficient to explain the (optional)

#pragma STDC CX_LIMITED_RANGE ON

of C99. That tells the compiler to use the obvious, and fast, formulae, and not to worry about the potential for overflow and underflow.

Signed Zeros

Problems with signed zeros are more subtle, and best illustrated with C99 again. Suppose one wishes to set the variable z to the value (-0)+i. In C99 one might imagine that

#include <complex.h>
double complex z=(-0)+I;

is the correct answer. Afterall, one must wait a dozen years for C11 before the macro CMPLX appears for constructing complex numbers.

But on every compiler I have access to in late 2025 the code

#include <stdio.h>
#include <complex.h>
 
int main(void)
{
    double complex z = (-0.0)+I;
    printf("z = %.1f%+.1fi\n", creal(z), cimag(z));
}

prints "0.0+1.0i". What makes this even worse is that a standard-conformant compiler could also print "-0.0+1.0i". And if the assignment is changed to (-0.0)-I then the only expected answer is "-0.0-1.0i", and changing the sign of the imaginary component has changed the sign of the real part too.

Multiplication

Everyone believes that

(a+ib)*(c+id)=(ac-bd)+(ad+bc)i.

But on computers which could perform additions and subtractions much more cheaply than multiplications

(a+ib)*(c+id)=(ac-bd)+[(a+b)(c+d)-ac-bd]i.

is more attractive. The first formula has four multiplications and two additions/subtractions, the second three multiplications and five additions/subtractions.

And on computers which have a fused multiply-add instruction, where FMA(a,b,c)=a*b+c, one might consider

(a+ib)*(c+id)=FMA(a,c,-bd)+FMA(a,d,bc)i.

but note that the FMA instruction rounds and overflows differently to the explicit multiply followed by add.

The Detail

Having sketched some of the problems, now the detail of the guarantees one receives in various languages, and also of what one might want.