Test program: // Copyright 2006, Google Inc. All rights reserved. // Author: [EMAIL PROTECTED] (Michael Chastain) // // Compute i = sqrt(complex<double>(-1,-0))
#include <complex> #include <iostream> std::complex<double> d_m1(-1.0, -0.0); int main() { std::cout << "d_m1: " << d_m1 << std::endl; std::complex<double> d_i_sqrt = std::sqrt(d_m1); std::cout << "d_i_sqrt: " << d_i_sqrt << std::endl; std::cout << std::endl; return 0; } === This gives different results with different versions of gcc. All of these are with glibc 2.3.5, which is important because libstdc++ calls down to glibc csqrt. hollerith:~/exp-i$ /home/mec/gcc-3.4.6/install/bin/g++ z2.cc && a.out d_m1: (-1,-0) d_i_sqrt: (0,1) hollerith:~/exp-i$ /home/mec/gcc-3.4.6/install/bin/g++ -O2 z2.cc && a.out d_m1: (-1,-0) d_i_sqrt: (0,1) hollerith:~/exp-i$ /home/mec/gcc-4.0.2/install/bin/g++ z2.cc && a.out d_m1: (-1,-0) d_i_sqrt: (0,-1) hollerith:~/exp-i$ /home/mec/gcc-4.0.2/install/bin/g++ -O2 z2.cc && a.out d_m1: (-1,-0) d_i_sqrt: (0,-1) hollerith:~/exp-i$ /home/mec/gcc-4.1.1/install/bin/g++ z2.cc && a.out d_m1: (-1,-0) d_i_sqrt: (0,-1) hollerith:~/exp-i$ /home/mec/gcc-4.1.1/install/bin/g++ -O2 z2.cc && a.out d_m1: (-1,-0) d_i_sqrt: (0,-1) hollerith:~/exp-i$ /home/mec/gcc-4.2-20060624/install/bin/g++ z2.cc && a.out d_m1: (-1,-0) d_i_sqrt: (0,-1) hollerith:~/exp-i$ /home/mec/gcc-4.2-20060624/install/bin/g++ -O2 z2.cc && a.out d_m1: (-1,-0) d_i_sqrt: (0,-1) === What is the correct value? ISO/IEC 14882 says: 26.2.8 [lib.complex.transcendentals] template<class T> complex<T> sqrt(const complex<T>& x); -12- Notes: the branch cuts are along the negative real axis. -13- Returns: the complex square root of x, in the range of the right half-plane. If the argument is a negative real number, the value returned lies on the positive imaginary axis. ISO/EIC 9899:TC2 says: 7.3.3 Branch cuts Some of the functions below have branch cuts, across which the function is discontinuous. For implementations with a signed zero (including all IEC 60559 implementations) that follow the specifications of annex G, the sign of zero distinguishes one side of a cut from another so the function is continuous (except for format limitations) as the cut is approached from either side. For example, for the square root function, which has a branch cut along the negative real axis, the top of the cut, with imaginary part +0, maps to the positive imaginary axis, and the bottom of the cut, with imaginary part 0, maps to the negative imaginary axis. And LIA-3, Draft ISO/IEC FCD 10967-3 First edition 2004-06-22, says: sqrtC(F)(x+i.y) = conjC(F)(sqrtC(F)(x+i.0)) if x is in F u {-inf, +inf} and y = -0 === ISO/EIC 14882 appears to require the (-0,1) answer, because (-1,-0) is a negative real number. Argh ... it depends ... on what "is" is. LIA-3 is clear but I don't know the standards status of LIA-3. -- Summary: What should be value of sqrt(complex<double>(-1.0,- 0.0))? Product: gcc Version: 4.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: mec at google dot com GCC build triplet: i686-pc-linux-gnu GCC host triplet: i686-pc-linux-gnu GCC target triplet: i686-pc-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28406