gpt4 book ai didi

c++ - sin() 未在 Visual Studio 2013 64 位上返回预期结果

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:50:15 26 4
gpt4 key购买 nike

在我开发的工程应用程序中,我偶然发现了 32 位和 64 位之间 sin(-0) 结果的差异。由于计算的性质,这会传播到一些相位差。

我们正在使用 MSVC 2013 在 Windows 上进行开发。

显然,浮点标准指定 sin(-0) 返回参数不变 - 根据 cppreference/sin至少。

我做了一些调查,这些是我得到的一些其他结果:

// Visual Studio 2013 32 bit on Win7 - default arguments
std::sin( -0 ) = -0
std::sin( 0 ) = 0

// Visual Studio 2013 64 bit on Win7 - default arguments
std::sin( -0 ) = 0 // the faulty one
std::sin( 0 ) = 0

// g++ (GCC) 5.1.0 : g++ -std=c++11 -O2 -Wall -pedantic -mfpmath=387 -m64 main.cpp && ./a.out
std::sin( -0 ) = -0
std::sin( 0 ) = 0

// g++ (GCC) 5.1.0 : g++ -std=c++11 -O2 -Wall -pedantic -mfpmath=sse -m64 main.cpp && ./a.out
std::sin( -0 ) = -0
std::sin( 0 ) = 0

我还知道英特尔数学库 (libm*.dll) 也返回 sin(-0)=-0。

查看反汇编,std::sin 的实现指向 msvcr120d.dll。

问题:

  • 这是 Microsoft 在 64 位上执行 sin 例程的错误吗?
  • 我是否应该使用一些我不知道的特定编译器参数?

用于上述输出的代码:

#include <cmath>
#include <iostream>

void printSin( const double dfPh )
{
const auto dfSinPh = std::sin( dfPh );
std::cout.precision( 16 );
std::cout << "std::sin( " << dfPh << " ) = " << dfSinPh << std::endl;
}

int main()
{
printSin( -0.00000000000000000000 );
printSin( +0.00000000000000000000 );
return 0;
}

最佳答案

最后我采用了穷人的解决方案。我使用 std::signbit 专门检查 -0 并接近零,并考虑 sin(-0) = -0。慢的?也许吧,但符合我们的需求。

关于c++ - sin(<minus zero>) 未在 Visual Studio 2013 64 位上返回预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30392832/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com