C and Complex Numbers

The first C standard to introduce complex numbers was C99. It includes an annex, Annex G, which defines quite precisely how they should work.

Whilst the C99 Standard requires support for complex types, the C11 and later Standards do not (I believe due to pressure from those concerned with embedded systems who did not want the overhead of supporting something of no relevance to them). So one immediate problem with C and complex numbers is whether one's compiler actually supports them. Almost all compilers designed for computers do, except Microsoft's.

Pure Imaginary

C is unusual in defining not only a complex data type, but also a pure imaginary data type. Unfortunately the imaginary type is an optional part of the optional complex support, and I have failed to find a compiler which actually supports it.

C99 defines the constant (macro) I to have the value of i. But the type of I will be imaginary if the compiler supports the imaginary type, else complex. C99 does not define any way of building a complex number from real and imaginary components other than

  double complex z=a+I*b;

Annex G does not take the Fortran approach with binary operators and complex arithmetic of first promoting both operands to the complex type. Rather it defines how to perform addition, multiplication and division with mixed types directly. In this case, if I is imaginary, then z is indeed set to (a,b). But if I is complex, then z is set to (a-0*b,b+0*a) as I=(0,1). If a was equal to -0, and b is negative, the results will differ, as (-0)-(-0)=+0.

The C11 Standard introduced the CMPLX macro, and

  double complex z=CMPLX(a,b);

does not hide any surprises.

A similar issue arises with

  z=CMPLX(-0,1);
  z=z*I;

If the compiler supports the imaginary type, this will be -1-0i. If it does not, it will be -1+0i. If that difference looks minor, consider z=-csqrt(z*I);, which will be either +i or -i.

Multiplication

Whereas Fortran permits a compiler to take the obvious short-cuts when multiplying a complex by a real, or dividing a complex by a real, or adding a real to a complex, Annex G requires them. Good for performance.

But very bad for performance is Annex G's approach to full complex-complex multiplication. It states that "if one operand is an infinity and the other operand is a nonzero finite number or an infinity, then the result of the * operator is an infinity" and "a complex value with at least one infinite part is regarded as an infinity (even if its other part is a NaN)."

So (Inf,0)*(Inf,Inf) must be an infinity. Unfortunately the traditional formula of

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

results in (NaN,NaN) as 0*Inf=NaN and anything+NaN=NaN. No better is (Inf,NaN)*(1,1). Annex G gives some example code for conformant complex-complex multiplication. It is about forty lines long, even if it "achieves the required treatment of infinities at the cost of only one isnan() test in ordinary (finite) cases." One isnan() test, followed by some complicated recovery, does not aid compilers attempting to vectorise, or to target GPUs. An inner loop with half a dozen complex-complex multiplications could become quite messy.

NaNs

In real arithmetic, a fundamental principle is that any operation returning a real will return a NaN if any of its operands is a NaN. Once a NaN enters a calculation, it stays until the end.

With Annex G complex arithmetic this is not so. If one component is a NaN, but the other is Inf, then the whole is considered to be an infinity, not a NaN, and some operations will give finite results. In particular, cexp(-Inf+iNaN) is defined to be zero and ctanh(Inf+iNaN) to be one. NaNs can be lost.

Even a simple multiplication can lose a NaN component, as

  (Inf+iNaN)*(Inf+iInf)=Inf+iInf

and the reciprocal of this is zero.

Summary

A C99 (or later) might, or might not, have complex support, and C11 is needed for the CMPLX macro. If it supports complex it might, but probably does not, support a pure imaginary type. It probably will support Annex G by default, which leads to very slow multiplication, and overflow-safe division and absolute values.

It should offer an escape route to the more conventional multiplication via

#pragma STDC CX_LIMITED_RANGE OFF

However gcc currently ignores this pragma (but supports an equivalent command-line option). The pragma also permits the fast, and overflow-prone, formulae for division and absolute values. One cannot have fast multiplication but safe division.