如何使用try ... catch捕捉浮点错误? [英] How do I use try...catch to catch floating point errors?

查看:272
本文介绍了如何使用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?

建议和/或指向一些文献的指南将不胜感激。
编辑:双变量中返回的值为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.

所以你要这样做:

#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)


}

这里的一些混乱可能是使用单词exception。在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.html
http://floating-point-gui.de/

Some additional floating-point references: http://docs.sun.com/source/806-3568/ncg_goldberg.html http://floating-point-gui.de/

这篇关于如何使用try ... catch捕捉浮点错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆