|
From: Keith M. <kei...@us...> - 2014-12-04 14:30:20
|
On 03/12/14 22:38, Graeme Paterson wrote: > Works in Ubuntu, not in mingw. Indeed, but it is *not* a GCC bug, (therefore not specific to any version of GCC, as distributed by MinGW.org); it is actually a problem in the complex *arcsine* function implementation in MinGW's runtime library (mingwrt). > Used to works in earlier mingw but I've upgraded, have lost the old > version. Really? That surprises me. The implementation was originally contributed by Danny Smith, in October 2003, and there is no record of it ever having been modified since. In what version did you think it might have worked? > #include <stdio.h> > #include <complex.h> > > int main() { > double complex A; > double complex a; > A = 1.1; > a = cacos( A ); // fails id abs(A) > 1.0 Indeed, this is where it first appears to go wrong, but it's actually in casin(A), (which is called by cacos(A)), that the failure occurs. In the source, I see: double complex casin (double complex Z) { double complex Res; double x, y; x = __real__ Z; y = __imag__ Z; if (y == 0.0) { __real__ Res = asin (x); __imag__ Res = 0.0; } else : which seems to be a sensible optimization, until you consider that values of complex sine may lie outside the valid bounds for the real asin(x) function; that's precisely what's happening in your case, and asin(x) is correctly returning NaN, given the out-of-bounds argument. > printf("%.12g %.12g\n", creal(a), cimag(a) ); > // prints '-1.#IND 0' wrong > a = cacos( 1.1 ); // calculated at compile time, OK > printf("%.12g %.12g\n", creal(a), cimag(a) ); > // prints '0 -0.443568254385' correct > return 0; > } Simply changing the `if (y == 0.0)', to also reject the optimization for `fabs(x) > 1.0' is almost, but not quite, enough to get me to the "correct" result you expect ... when performing the computation, the casin(Z) function is returning an imaginary part with opposite sign to that which might be expected, and this propagates in the result which is returned by cacos(Z). I guess a formal review of the entire complex math suite in mingwrt, by an expert mathematician would be useful. Any volunteers? -- Regards, Keith. |