提问人:user1139069 提问时间:1/22/2023 最后编辑:user1139069 更新时间:1/22/2023 访问量:105
为什么在 std::vector 内部使用 std::complex<double> 时,机器 epsilon 的阶数会有所不同?
Why am I getting a difference on the order of machine epsilon when using std::complex<double> from inside a std::vector?
问:
在尝试调试一些单元测试时,我发现在尝试比较使用 .在第一种情况下,我直接进行评估。在第二种情况下,我从内部访问这些。当我比较它们时,我得到了使用时机器 epsilon 的顺序差异。最小大小写示例如下所示std::pow
std::complex<double>
std::pow
std::vector
-O3
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)
这是在 AMD EPYC 7713 64 核处理器上运行的。使用 、 或 时未看到此错误。float
double
std::complex<float>
使用 gcc 12.1.0 重新测试显示相同的错误。
(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)
答: 暂无答案
上一个:Python 中的浮点近似
下一个:浮点数学坏了吗?
评论
(0,0)
std::pow(std::complex(4.0), std::complex(-0.23))
std::pow(base.at(0), exp.at(0))
cpow
-frounding-math
cpow
std::pow