While trying to debug some unit tests, I found I'm getting a difference when trying to compare the results of std::pow
using std::complex<double>
. In the first case, I'm evaluating std::pow
directly. In the second case, I'm accessing these from inside a std::vector
. When I compare them, I get a difference on the order of machine epsilon when -O3
is used. The minimum case example looks like
perlmutter:~/bug> cat test.cpp
#include <complex>
#include <iostream>
#include <vector>
int main(int argc, const char * argv[]) {
std::vector<std::complex<double>> base(1, 4.0);
std::vector<std::complex<double>> exp(1, -0.23);
std::cout << std::pow(std::complex(4.0), std::complex(-0.23)) -
std::pow(base.at(0), exp.at(0)) << std::endl;
return 0;
}
perlmutter:~/bug> /opt/cray/pe/gcc/11.2.0/bin/c++ --version
c++ (GCC) 11.2.0 20210728 (Cray Inc.)
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
perlmutter:~/bug> /opt/cray/pe/gcc/11.2.0/bin/c++ -O3 -o test test.cpp
perlmutter:~/bug> ./test
(1.11022e-16,-0)
This is being run on an AMD EPYC 7713 64-Core Processor. I don't see this error when using float
, double
, or std::complex<float>
.
Retesting with gcc 12.1.0 shows the same error.
(base) perlmutter:~/bug> gcc --version
gcc (GCC) 12.1.0 20220506 (HPE)
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
perlmutter:~/bug> g++ -O3 -o test test.cpp
perlmutter:~/bug> ./test
(1.11022e-16,-0)