c++ - _controlfp_s 中的可能错误可能无法正确恢复控制字

标签 c++ visual-studio visual-studio-2008 visual-c++

我偶然发现了 _controlfp_s (Visual Studio 2008) 的问题,或者我对它的理解。我认为第一个输出参数在应用其他参数的更改之前返回了控制标志。似乎它在更改后返回标志。

所以,我认为正确的使用方法是这样的:

// Chop rounding
unsigned int old;
_controlfp_s(&old, _RC_CHOP, _MCW_RC);

// Do chopped math


// Restore
unsigned int unused;
_controlfp_s(&unused, old, _MCW_RC);

不幸的是我需要这样做:

// Save
unsigned int old1;
_controlfp_s(&old1, 0, 0);

// Chop rounding
unsigned int old2;
_controlfp_s(&old2, _RC_CHOP, _MCW_RC);

// Do chopped math


// Restore
unsigned int unused;
_controlfp_s(&unused, old1, _MCW_RC);

我错过了什么吗?必须这样做似乎很愚蠢。

顺便说一句:我已将此报告给 MS,MS 表示他们无法理解并建议我提供显示该问题的视频。没错。

布拉德

最佳答案

根据 MSDN :

If the value for mask is equal to 0, _controlfp_s gets the floating-point control word. If mask is nonzero, a new value for the control word is set: For any bit that is on (equal to 1) in mask, the corresponding bit in new is used to update the control word. In other words, fpcntrl = ((fpcntrl & ~mask) | (new & mask)) where fpcntrl is the floating-point control word.

(强调我的) 因此,可靠地存储当前控制字的方法是您编写的第二种方法(您已经发现有效的方法)。如果您要修改控制字,则不会为掩码传递 0,并且根据函数文档,它不会检索当前控制字。

关于c++ - _controlfp_s 中的可能错误可能无法正确恢复控制字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/637175/

相关文章:

c++ - 将模板类实例传递给另一个类的构造函数

c++ - 如何调试输出指向成员的指针?

c++ - lambda 的 const auto 和 auto 之间的区别

visual-studio - Nuget 自动还原 - 为 TFS 构建指定自定义存储库

c# - 为什么在 Asp :GridView? 中有不需要的重复列和额外列

c++ - 任何在 C++ 流上执行 printf 的库?

c# - Visual C# 在 Appdata 中存储和读取自定义 XML 中的自定义选项

visual-studio - 在 Visual Studio 中搜索时仅包含某些文件类型

c# - 如何在Winform中使用CSS格式化 Crystal 报表?

c# - IIS调试在Windows 2012服务器IIS 8.0