Stl: <complex>: std::arg does not work for negative real values

Created on 4 May 2020  路  5Comments  路  Source: microsoft/STL

Describe the bug
I'm trying to do some simple phase calculations in C++ and discovered that I was sometimes getting the wrong results from std::arg when using real values as input.

According to the documentation on cppreference:

Additional overloads are provided for float, double, long double, and all integer types, which are treated as complex numbers with zero imaginary component.

However this does not seem to be the case, because these two calls return different results.

auto x1 = std::arg(std::complex<double>{ -1.0, 0.0 });
// x1 = PI (3.14159...)

auto x2 = std::arg(-1.0);
// x2 = 0

Command-line test case

#include <complex>
#include <iostream>

int main() {
    auto x1 = std::arg(std::complex<double>{ -1.0, 0.0 });
    auto x2 = std::arg(-1.0);

    if (x1 != x2)
    {
        std::cout << "test failure" << std::endl;
    }
}

Expected behavior
I would expect std::arg to function as described in the documentation. When passed a real value as an input it should act the same as if it were passed a complex value with a zero imaginary component.

STL version
Microsoft Visual Studio Community 2019 Preview
Version 16.6.0 Preview 5.0

Additional Context
Looking at the source code it looks like when passed a real value std::arg is hardcoded to just return 0.

bug fixed

All 5 comments

There were so many bugs in these overloads 馃檧 (as @CaseyCarter noticed) but I'm fixing them all 馃樃

Thanks again for reporting this bug! The fix will indeed ship in VS 2019 16.7 Preview 2.

Awesome! Thanks for the super quick turnaround! :)

For the record, this was also tracked by DevCom-125537 and Microsoft-internal VSO-507908.

Was this page helpful?
0 / 5 - 0 ratings