<legend id='J42pf'><style id='J42pf'><dir id='J42pf'><q id='J42pf'></q></dir></style></legend>
  1. <tfoot id='J42pf'></tfoot>

    • <bdo id='J42pf'></bdo><ul id='J42pf'></ul>
    1. <small id='J42pf'></small><noframes id='J42pf'>

      <i id='J42pf'><tr id='J42pf'><dt id='J42pf'><q id='J42pf'><span id='J42pf'><b id='J42pf'><form id='J42pf'><ins id='J42pf'></ins><ul id='J42pf'></ul><sub id='J42pf'></sub></form><legend id='J42pf'></legend><bdo id='J42pf'><pre id='J42pf'><center id='J42pf'></center></pre></bdo></b><th id='J42pf'></th></span></q></dt></tr></i><div id='J42pf'><tfoot id='J42pf'></tfoot><dl id='J42pf'><fieldset id='J42pf'></fieldset></dl></div>

      我如何使用 try...catch 来捕获浮点错误?

      How do I use try...catch to catch floating point errors?(我如何使用 try...catch 来捕获浮点错误?)
      <legend id='267TJ'><style id='267TJ'><dir id='267TJ'><q id='267TJ'></q></dir></style></legend>
        <bdo id='267TJ'></bdo><ul id='267TJ'></ul>

          <small id='267TJ'></small><noframes id='267TJ'>

        • <tfoot id='267TJ'></tfoot>

          1. <i id='267TJ'><tr id='267TJ'><dt id='267TJ'><q id='267TJ'><span id='267TJ'><b id='267TJ'><form id='267TJ'><ins id='267TJ'></ins><ul id='267TJ'></ul><sub id='267TJ'></sub></form><legend id='267TJ'></legend><bdo id='267TJ'><pre id='267TJ'><center id='267TJ'></center></pre></bdo></b><th id='267TJ'></th></span></q></dt></tr></i><div id='267TJ'><tfoot id='267TJ'></tfoot><dl id='267TJ'><fieldset id='267TJ'></fieldset></dl></div>

                <tbody id='267TJ'></tbody>

                本文介绍了我如何使用 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/

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

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                Delay Loading DLLs(延迟加载 DLL)
                Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                Loading a dll from a dll?(从 dll 加载 dll?)

                1. <i id='SD0pQ'><tr id='SD0pQ'><dt id='SD0pQ'><q id='SD0pQ'><span id='SD0pQ'><b id='SD0pQ'><form id='SD0pQ'><ins id='SD0pQ'></ins><ul id='SD0pQ'></ul><sub id='SD0pQ'></sub></form><legend id='SD0pQ'></legend><bdo id='SD0pQ'><pre id='SD0pQ'><center id='SD0pQ'></center></pre></bdo></b><th id='SD0pQ'></th></span></q></dt></tr></i><div id='SD0pQ'><tfoot id='SD0pQ'></tfoot><dl id='SD0pQ'><fieldset id='SD0pQ'></fieldset></dl></div>
                    <tbody id='SD0pQ'></tbody>
                    <tfoot id='SD0pQ'></tfoot>

                    <small id='SD0pQ'></small><noframes id='SD0pQ'>

                      • <bdo id='SD0pQ'></bdo><ul id='SD0pQ'></ul>

                      • <legend id='SD0pQ'><style id='SD0pQ'><dir id='SD0pQ'><q id='SD0pQ'></q></dir></style></legend>