Sanity and Complex Numbers

The complex type is very useful. Solving Schrodinger's Equation, or performing FFTs, are much easier with it. However, extending IEEE-754 to it is not easy, or, arguably, sane.

C99 with Annex G introduces some interesting inconsistencies. It claims that "a complex or imaginary value with at least one infinite part is regarded as an infinity", but also makes it clear that two complex values are not regarded as equal unless both their real and imaginary parts are equal. So Inf+2i and Inf+3i are not equal.

To be fair to C99, every language treats the equality of complex numbers like this. But if one could convince a mathematician that +Inf=+Inf, then attempting to extend the argument to Inf+2i being unequal to Inf+3i is unlikely to be successful.

The real and imaginary operators cause confusion. Are these storage access operations, or mathematical operations? The complex number NaN+2i is surely a NaN, and therefore its imaginary part is mathematically NaN, even if stored as 2.

  imag(sqrt(-4.0)+cmplx(0,2))

and

  imag(sqrt(cmplx(-4,0))+cmplx(0,2))

suitably converted to your favourite language will generally give the answers 2 and 4, as the first forms the complex value NaN+2i.

What are the values of

  1.0*Inf
  1.0*cmplx(Inf)
  cmplx(1.0)*cmplx(Inf)
  cmplx(1.0*Inf)

and would they compare as equal in your favourite language?

program t
  complex(kind(1d0)):: z

  z=cmplx(-1.0,-0.0)

  print*,sqrt(z)
  print*,sqrt(z+0)
  print*,sqrt(z+cmplx(0.0,0.0))
end program t
#include<stdio.h>
#include<complex.h>
int main(){
  double complex z=CMPLX(-1.0,-0.0),z2;
  z2=csqrt(z);
  printf("%f%+fi\n",creal(z2),cimag(z2));
  z2=csqrt(z+0.0);
  printf("%f%+fi\n",creal(z2),cimag(z2));
  z2=csqrt(z+CMPLX(0.0,0.0));
  printf("%f%+fi\n",creal(z2),cimag(z2));
  return 0;
}

If using gfortran, the above will produce -i, +i, +i. If using C99 Annex G it will produce -i, -i, +i. A standard-compliant Fortran compiler could produce -i followed by any combination of +i and -i. One might even try to argue that +i could be correct for the first result.

When dealing with functions with branch cuts, such as sqrt, the difference between +0 and -0 can be important. In many languages, with x real and z complex

  x=1
  z=x;
  z=-z;
  print sqrt(z)

and

  x=1
  x=-x
  z=x
  print sqrt(z)

are guaranteed to give different answers. Here gfortran and C99 Annex G both give -i then i.

Despite the restrictions of C99's Annex G, some things are still ill-defined.

csqrt(I*CMPLX(-0.0,1.0))

could be +i or -i depending on whether I is imaginary or complex.

Conclusion

One can have speed, and nothing like consistency. Or one can sacrifice speed, and still have imperfect consistency. For the work I do, speed matters a lot. The sort of consistency that is being considered here does not matter at all. So I would take the traditional Fortran model. Others might disagree, but do they really get as much consistency as they claim?

One problem is that IEEE-754 provides for two zeros. Really three would be better: +ε, 0 and -ε. Real zero is unchanged by multiplication by -1, whereas +ε changes to -ε. Real zero when added to -ε would leave -ε. Etc. IEEE-754 forces 0 and +ε to be the same, and they are not. This then causes surprises around branch cuts.

Why the mess? Some compilers replace complex numbers by their real and imaginary components early in their processing. This is in many ways sensible, and it simplifies complex support. But it does mean that the components then get treated according to IEEE-754 rules, including issues such as adding zero not being a redundant operation as it will flip the sign of -0. It also means that telling the compiler not to do this is likely to involve turning IEEE-754 off for real arithmetic too, possibly throughout the whole source file. This may be a little more cavalier than the programmer wanted.