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

查看:123
本文介绍了如何使用try ... catch来捕捉浮点错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



因为它们是随机的,所以树常常是生成:除以零,溢出,下溢以及返回inf等字符串。我可以为字符串编写处理程序,但文献让我对其他人感到困惑。如果我明白了,我必须首先设置一些标志?



建议和/或指向某些文献的指针将不胜感激。
编辑:双变量中返回的值为1.#INF或-1。#IND。我错了调用他们的字符串。

解决方案

你确定要抓住他们,而不是忽略它们吗?假设你只是想忽略它们:



看到这个:
http://msdn.microsoft.com/en-us/library/c9676k6h.aspx



对于_MCW_EM掩码,清除掩码设置异常,这允许硬件异常;设置面具隐藏异常。



所以你要做这样的事情:

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

//使新的fp env与旧的一样,
//除了我们要做的更改
new_fp_control_word = fp_control_word | _EM_INVALID | _EM_DENORMAL | _EM_ZERODIVIDE | _EM_OVERFLOW | _EM_UNDERFLOW | _EM_INEXACT;
//使用我们的更改更新控制字
_controlfp_s(& fp_control_word,new_fp_control_word,_MCW_EM)


}

这里的一些混乱可能是使用异常一词。在C ++中,通常是指内置异常处理系统的语言。浮点异常是一个不同的野兽。标准FPU需要支持的例外都在IEEE-754中定义。这些发生在浮点单元内,这可以根据浮点单元的控制标志如何设置而做不同的事情。通常发生两件事之一:
1)异常被忽略,FPU设置一个标志,指示其状态寄存器中发生错误。
2)FPU不会忽略该异常,因此中断将被生成,并且任何设置了用于浮点错误的中断处理程序被调用。通常这样做对你来说很不错,就像让你在调试器中的那一行代码断开或者生成一个核心文件一样。



您可以在这里找到更多IEE-754: 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/


I'm using c++ in visual studio express to generate random expression trees for use in a genetic algorithm type of program.

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?

Advice and/or a pointer to some literature would be appreciated. Edit: 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:

See this: http://msdn.microsoft.com/en-us/library/c9676k6h.aspx

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)


}

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.

You can find more on IEE-754 here: http://www.openwatcom.org/ftp/devel/docs/ieee-754.pdf

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天全站免登陆