clang can't do complex arithmetic

Steve Kargl sgk at troutmask.apl.washington.edu
Tue Nov 2 03:34:21 UTC 2010


It seems that clang can't do complex arithmetic.
Not to worry gcc in base can't do it either.

/*
 * The C99 standard intends x+I*y to be used for this, but x+I*y is
 * currently unusable because gcc introduces many overflow,
 * underflow, sign and efficiency bugs by rewriting I*y as
 * (0.0+I)*(y+0.0*I) and laboriously computing the full complex product.
 * In particular, I*Inf is corrupted to NaN+I*Inf, and I*-0 is corrupted
 * to -0.0+I*0.0.
 */
#include <complex.h>
#include <math.h>
#include <stdio.h>

int main(void) {

  double complex z;
  double x, y;

  x = 0.;
  y = 1. / x;
  x = copysign(x, -1.);

  /* z = 0 + i (-0) */
  z = I * x;
  printf("z = 0 + i (-0) = %e + i (%e)\n", creal(z), cimag(z));

  /* z = 0 + i Inf */
  z = I * y;
  printf("z = 0 + i Inf = %e + i %e\n", creal(z), cimag(z));

}

troutmask:sgk[204] clang -o z z.c -lm && ./z
z = 0 + i (-0) = -0.000000e+00 + i (0.000000e+00)
z = 0 + i Inf = nan + i inf


If I read Annex G in n1256.pdf correctly, the z = I*inf = NaN + I inf
is going to really bad things because the NaN is going to propagate
if z is used in further computations.  Annex G says z is an infinity.

-- 
Steve


More information about the freebsd-toolchain mailing list