zoukankan      html  css  js  c++  java
  • 在vs2005/c++中捕获浮点数异常

    vs2005项目的缺省属性是忽略float异常的,要想在代码中捕获float异常按以下方法.

    分三步:
    1,

    //Set the x86 floating-point control word according to what
    //exceptions you want to trap. 
    _clearfp(); //Always call _clearfp before setting the control
                
    //word
    //Because the second parameter in the following call is 0, it
    //only returns the floating-point control word
    unsigned int cw = _controlfp(00); //Get the default control
                                        
    //word
    //Set the exception masks off for exceptions that you want to
    //trap.  When a mask bit is set, the corresponding floating-point
    //exception is //blocked from being generating.
    cw &=~(EM_OVERFLOW|EM_UNDERFLOW|EM_ZERODIVIDE|
           EM_DENORMAL
    |EM_INVALID);
    //For any bit in the second parameter (mask) that is 1, the 
    //corresponding bit in the first parameter is used to update
    //the control word.  
    unsigned int cwOriginal = _controlfp(cw, MCW_EM); //Set it.
                                
    //MCW_EM is defined in float.h.
                                
    //Restore the original value when done:
                                
    //_controlfp(cwOriginal, MCW_EM);

    2,option==>c/c++==>code generation==>Enable c++ Exception改为/EHa

    3,

    try
    {
         
    //Code that may generate a floating-point exception
    }
    catch(. . .)
    {
         
    int fpStatusWord =  _clear87();
         
    //You may analyze the bits of the status word.
         
    //Code that does appropriate processing
    }


     

  • 相关阅读:
    Python实例 包机制
    Python实例 类和继承
    python实例 文件处理
    python实例 异常处理
    配置 Apache+php多端口多站点(转载)
    C#中Delegate和Event以及它们的区别(转载)
    LINQ to SQL更新数据库操作(转载)
    创业公司如何实施敏捷开发(转)
    利用ps橡皮擦工具快速抠图
    XP win2003系统 微软雅黑字体的使用方法
  • 原文地址:https://www.cnblogs.com/corefans/p/1354897.html
Copyright © 2011-2022 走看看