https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61761
Bug ID: 61761 Summary: [C++11] std::proj returns incorrect values Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: libstdc++ Assignee: unassigned at gcc dot gnu.org Reporter: kariya_mitsuru at hotmail dot com std::proj returns incorrect values if _GLIBCXX_USE_C99_COMPLEX is not defined. For example, std::proj(std::complex<double>(1, 2)) should be std::complex<double>(1, 2) but libstdc++ returns std::complex<double>(0.333333,0.666667). If _GLIBCXX_USE_C99_COMPLEX is defined, the sample code below can show the difference between a glibc version and a libstdc++ version. ============ sample source ======================================== #include <iostream> #include <complex> #include <climits> void check(const std::complex<double>& c) { // libstdc++ version std::complex<double> r1 = std::__complex_proj(c); std::cout << "libstdc++ version : proj( " << c << " ) = " << r1 << std::endl; // glibc version std::complex<double> r2 = std::__complex_proj(c.__rep()); std::cout << "glibc version : proj( " << c << " ) = " << r2 << std::endl; } int main() { check(std::complex<double>(1, 2)); check(std::complex<double>(1, NAN)); } ============ sample output ======================================== libstdc++ version : proj( (1,2) ) = (0.333333,0.666667) glibc version : proj( (1,2) ) = (1,2) libstdc++ version : proj( (1,nan) ) = (nan,nan) glibc version : proj( (1,nan) ) = (1,nan) =================================================================== According to C99 standard 7.3.9.4 (referenced from C++11 standard 26.4.7 [complex.value.ops] paragraph 8), "z projects to z except that all complex infinities (even those with one infinite part and one NaN part) project to positive infinity on the real axis." Note that the glibc version 2.11 or below had the same bug. (For std::complex<double>(1, NAN), the glibc version 2.18 or below had a different bug.)