IEEE-754 and Complex Numbers

Most current computers follow a standard called IEEE-754 (also known as ISO/IEC 60559) for real arithmetic. It defines multiple precisions, and special values such as infinity and NaN (not a number). Slightly surprisingly it defines two zeros, +0 and -0. Some of the rules it specifies are:

It also specifies that the result of any arithmetic operator when one argument is a NaN is a NaN, and of any conditional operator is false. So the expression (x==x) is false if x is a NaN.

But IEEE-754 says nothing about complex arithmetic. Some languages define how complex arithmetic is to be performed, such as in Annex G of the C99 standard. Others do not. If one is left to guess, there are all sorts of sources of confusion.

Infinities and NaNs

Mathematicians usually regard the complex domain as having a single point at infinity, and there is only a single NaN. Computationally there are many representations of these. For instance, 6+Inf*i and Inf+4i are both representations of the point at infinity.

It is therefore best if one regards the functions real() and imag() for extracting the real and imaginary parts of a complex number as precisely that -- storage access functions. They are not mathematical projections onto the real line. If they were, real(6+Inf*i) would have to evaluate to Inf, or perhaps NaN, and certainly not to 6.

Similar arguments apply to NaN.

  real x=sqrt(-4.0)
  complex z=complex(x,1)

After this pseudo-code, most languages will report that real(z) is NaN, and imag(z) is one. Mathematically one might argue that real(z) is zero, and imag(z) is three. Mathematically the imaginary component is not isolated from the NaN in the real component.

Multiplication by Reals

If one represents a complex number by a pair of reals, then

(a,b)*(c,d)=(ac-bd,bc+ad).

But if b is known to be zero, perhaps because the first number is actually real, can one rewrite this as

(a,0)*(c,d)=(ac,ad)?

Consider some examples

  1. (3,0)*(2,NaN)=(NaN,NaN) or (6,NaN)
  2. (3,0)*(2,Inf)=(NaN,Inf) or (6,Inf)
  3. (3,0)*(2,-0)=(6,0) or (6,-0)

The first is easily excused. Mathematically there is one NaN, so both answers are NaN.

The second is trickier, but the C standard makes it clear that if one component is Inf, then the whole should be considered to be Inf, even if the other component is NaN. This will surely cause surprises in other situations. It suggests that NaN+(2,Inf)=(NaN,Inf) is an infinity, but NaN+(Inf,2)=(NaN,2) is a NaN.

The third is also easy to excuse, as +0 and -0 are defined to compare equal.

But there is no doubt that the answers are computationally distinct, and operations such as real() will produce different results. The idea of IEEE-754 is that arithemetic should be highly reproducible, giving identical answers with different platforms and compilers, and this does not look reproducible.

More Signed Zeros

What is 3+(2-0i)? Is it 5-0i or 5+0i? If the former, what is (3+0i)+(2-0i)?

Signed zeros produce all sorts of optimisation surprises. x+0 is not identical to x when x=-0. x-y is not identical to -(y-x) if x=y. Fortran may have an answer to this. (The Fortran 2023 Standard section 17.9 states "complete conformance with ISO/IEC 60559:2020 is not required, but [...] the intrinsic operations of addition, subtraction and multiplication shall conform whenever the operands and result specified by ISO/IEC 60559:2020 are normal numbers." Zero is not a normal number according to the definitions section of IEEE-754, and whilst the Fortran standard says more about infinities and NaNs, which are also not normal numbers, it does not say anything explicitly about the addition of signed zeros.)

Overflows and Underflows

Above we asserted that

(a,b)*(c,d)=(ac-bd,bc+ad).

This can be problematic if ac produces an overflow, but ac-bd would not. Indeed, the representable space of complex numbers is a square in the complex plane. So multiplying a number by one of the roots of units (i.e. a number whose modulus is one) may necessarily produce an overflow, which is slightly surprising.

Division is harder. The mathematical identity

(a,b)/(c,d)=(a,b)*(c,-d)/(c*c+d*d)

is very prone to overflows and underflows in the demoninator. There are many ways of improving this, but they are all computationally much more expensive. There is little guidance on what algorithms might be considered acceptable here.

How Does One Multiply?

(a+ib)*(c+id)=(a*c-b*d)+(a*d+b*c)i

Four multiplies, two adds, and surely no arguments?

Not quite. Multiplies are more expensive than adds on some hardware (although not on almost all current hardware). So one might consider.

t=a*(c+d)
(a+ib)*(c+id)=t-d*(a+b)+[t+c*(b-a)]i

Three multiplies, five adds.

Or recalling that modern hardware often has a fused multiply add (FMA) instruction which calculates x+y*z in one operation, with no intermediate rounding, there are other options such as

(a+ib)*(c+id)=fma(-b*d,a,c)+fma(b*c,a,d)i

Although these formulae are algebraically identical, they will not give identical results in all cases when faced with finite precision arithmetic, or IEEE's special values. As one example

(4+4i)*(Inf+2i)=Inf+Inf*i (first method)
(4+4i)*(Inf+2i)=Inf+NaN*i (second method)

Mathematicians might wonder why one should not rewrite the two operands in a different form, and say that

(p*eiθ)*(q*eiφ)=(p*q)*ei(θ+φ)

Whilst perfectly valid, this would be very computationally expensive, as converting between the a+ib and p*exp(iθ) forms for expressing a complex number involves slow trigonometric functions. If it happens that one is storing the number in modulus/argument form, then this method might be good, but most languages do not do this, not least because it makes addition hard.

Note that the product (1+2i)*(Inf+Inf*i) is NaN+Inf*i by the first two methods, but NaN+NaN*i by the modulus/argument method.

Unless a language mandates a particular method of complex-complex multiplication, IEEE-754, which says nothing about the subject, does cannot judge between these answers.

Conclusion

Annex G of the C99 standard specifies one way of extending IEEE-754 to cover the complex type. But it is an extrapolation of what is written in IEEE-754, and whilst not without merit, not the only possible approach. A language which specifies nothing should not be assumed to mandate this approach, nor any other.

Annex G is not perfect, in that with x real and z complex, x*z may differ from ((complex)x)*z, for instance if z=(2,Inf). But such surprises are probably inevitable.

In 2024, J3, the Fortran Standards Committee, produced a clarification to how this matter should be treated in Fortran. The brief summary is "Fortran does not specify the method of rounding, approximating, or computing numeric values except by reference to [IEEE-754]. [IEEE-754] contains no specification of complex arithmetic. [...] The Fortran standard itself contains no specification or requirement on the algorithm used to calculate complex multiplication. [...] As the standard contains no specification or requirement, it thus contains no requirement that the same algorithm be used at all times. [...] It is thus valid for the processor to use a different algorithm for complex multiply when the operation is on an odd-numbered line of the source file, the operation involves a real value promoted to complex, or on alternate Tuesdays."

The bottom lines are: