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

标签 c++ visual-c++ visual-studio-2013 ieee-754

在我开发的工程应用程序中,我偶然发现了 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/

相关文章:

c++ - std::unordered_map 是如何实现的

winapi - MFTransform 编码器->ProcessInput 返回 E_FAIL

c++ - 从 SDL_Texture 访问像素颜色

winapi - 请求刷新 LVN_GETEMPTYMARKUP

c++ - 返回实例导致 'attempting to reference a deleted function' 错误

c# - 在 C# 中将 JSON 字符串解析为 JSON 对象,而无需编写额外的对象类

c++ - incredibuild 不使用其他代理

visual-studio - Visual Studio 2013 : how to open multiple projects in different windows?

c++ - 如何确定进程是 32 位还是 64 位?

c++ - 双指针和它们之间的括号