Consider the following code:
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <complex.h> int main() { complex double aaa = INFINITY + 0*I; printf("%.f + %.f*I\n", creal(aaa), cimag(aaa)); complex double bbb = 1.0/aaa; printf("%.f + %.f*I\n", creal(bbb), cimag(bbb)); return EXIT_SUCCESS; }
Compiling with gcc -std=gnu99 -lm
, I expect the output to be
inf + 0 * I
0 + 0 * I
which is true for Linux (tested on Scientific Linux 6.8 with gcc 4.4.7, Fedora 23 with gcc 5.3.1 and Ubuntu 14.04.5 with gcc 4.8.4).
However, on OS X (10.11.5 with clang-602.0.53), instead I get
inf + 0 * I
nan + nan * I
Obviously, clang does not comply with the C99 standard (see N1256 , section G.5.1, strictly speaking, this is just a recommended practice, not a standard). In fact, clang does not have a defined macro __STDC_IEC_559_COMPLEX__
, which is introduced in Sec. G.1. Is clang intentionally doing this? UPDATE: after some checks, I found that in some Linux environments that I tested, this macro is not defined, but the code still works correctly.
My cross-platform support solution now is macro validation
#include <stdio.h> #include <stdlib.h> #include <math.h> #include <complex.h> int main() { complex double aaa = INFINITY + 0*I; printf("%.f + %.f*I\n", creal(aaa), cimag(aaa)); complex double bbb = 1.0/aaa; #ifndef __STDC_IEC_559_COMPLEX__ if(isnan(bbb)) { bbb = 0; //or do some trick that has to do with the problem context } #endif printf("%.f + %.f*I\n", creal(bbb), cimag(bbb)); return EXIT_SUCCESS; }
But I'm not sure how durable it is ... Any suggestion?