我如何使用 try...catch 来捕获浮点错误?
我在 Visual Studio Express 中使用 C++ 来生成用于遗传算法类型程序的随机表达式树.
I'm using c++ in visual studio express to generate random expression trees for use in a genetic algorithm type of program.
因为它们是随机的,所以树经常产生:除以零、上溢、下溢以及返回inf"等字符串.我可以为字符串编写处理程序,但是文献让我对其他的感到困惑.如果我理解正确,我必须先设置一些标志吗?
Because they are random, the trees often generate: divide by zero, overflow, underflow as well as returning "inf" and other strings. I can write handlers for the strings, but the literature left me baffled about the others. If I understand it correctly, I have to set some flags first?
建议和/或指向某些文献的指针将不胜感激.double 变量中返回的值是 1.#INF 或 -1.#IND.称它们为字符串是错误的.
Advice and/or a pointer to some literature would be appreciated. the values returned in the double variable are 1.#INF or -1.#IND. I was wrong to call them strings.
推荐答案
您确定要抓住它们而不是忽略它们吗?假设您只想忽略它们:
Are you sure you want to catch them instead of just ignoring them? Assuming you just want to ignore them:
看到这个:http://msdn.microsoft.com/en-us/library/c9676k6h.aspx
对于_MCW_EM掩码,清除掩码设置异常,允许硬件异常;设置掩码会隐藏异常.
For the _MCW_EM mask, clearing the mask sets the exception, which allows the hardware exception; setting the mask hides the exception.
所以你会想要做这样的事情:
So you're going to want to do something like this:
#include <float.h>
#pragma fenv_access (on)
void main()
{
unsigned int fp_control_word;
unsigned int new_fp_control_word;
_controlfp_s(&fp_control_word, 0, 0);
// Make the new fp env same as the old one,
// except for the changes we're going to make
new_fp_control_word = fp_control_word | _EM_INVALID | _EM_DENORMAL | _EM_ZERODIVIDE | _EM_OVERFLOW | _EM_UNDERFLOW | _EM_INEXACT;
//Update the control word with our changes
_controlfp_s(&fp_control_word, new_fp_control_word, _MCW_EM)
}
这里的一些混淆可能与例外"一词的使用有关.在 C++ 中,这通常是指语言内置的异常处理系统.浮点异常是完全不同的野兽.标准 FPU 需要支持的例外都在 IEEE-754 中定义.这些发生在浮点单元内部,根据浮点单元的控制标志的设置方式,浮点单元可以做不同的事情.通常会发生以下两种情况之一:1) 异常被忽略,FPU 设置一个标志,指示在其状态寄存器中发生了错误.2) FPU 不会忽略异常,因此会生成中断,并且会调用为浮点错误设置的任何中断处理程序.通常这对你有好处,比如让你在调试器中的那行代码处中断或生成一个核心文件.
Some of the confusion here may be over the use of the word "exception". In C++, that's usually referring to the language built-in exception handling system. Floating point exceptions are a different beast altogether. The exceptions a standard FPU is required to support are all defined in IEEE-754. These happen inside the floating-point unit, which can do different things depending on how the float-point unit's control flags are set up. Usually one of two things happens: 1) The exception is ignored and the FPU sets a flag indicating an error occurred in its status register(s). 2) The exception isn't ignored by the FPU, so instead an interrupt gets generated, and whatever interrupt handler was set up for floating-point errors gets called. Usually this does something nice for you like causing you to break at that line of code in the debugger or generating a core file.
您可以在此处找到有关 IEE-754 的更多信息:http://www.openwatcom.org/ftp/devel/docs/ieee-754.pdf
You can find more on IEE-754 here: http://www.openwatcom.org/ftp/devel/docs/ieee-754.pdf
一些额外的浮点引用:http://docs.sun.com/source/806-3568/ncg_goldberg.htmlhttp://floating-point-gui.de/
相关文章