c++ float型 除0和溢出

colinares 2016-11-23 10:24:08
运行时,出现除0问题,有些pc不中断报错,产生1.#INF,但有些pc给出了中断报错,提示除0或者溢出?
能否通过设置消除这个提示而不是修改代码判断0值。
...全文
575 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 2016-11-23
  • 打赏
  • 举报
回复
使用__try捕获EXCEPTION_FLT_OVERFLOW、EXCEPTION_FLT_DIVIDE_BY_ZERO异常
赵4老师 2016-11-23
  • 打赏
  • 举报
回复
仅供参考:
_fpreset
Resets the floating-point package.

void _fpreset( void );

Function Required Header Compatibility 
_fpreset <float.h> Win 95, Win NT 


For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version 
LIBCMT.LIB Multithread static library, retail version 
MSVCRT.LIB Import library for MSVCRT.DLL, retail version 


Return Value

None

Remarks

The _fpreset function reinitializes the floating-point math package. _fpreset is usually used with signal, system, or the _exec or _spawn functions. If a program traps floating-point error signals (SIGFPE) with signal, it can safely recover from floating-point errors by invoking _fpreset and using longjmp.

Example

/* FPRESET.C: This program uses signal to set up a
 * routine for handling floating-point errors.
 */

#include <stdio.h>
#include <signal.h>
#include <setjmp.h>
#include <stdlib.h>
#include <float.h>
#include <math.h>
#include <string.h>

#pragma warning(disable : 4113) /* C4113 warning expected */

jmp_buf mark;              /* Address for long jump to jump to */
int     fperr;             /* Global error number */

void __cdecl fphandler( int sig, int num );   /* Prototypes */
void fpcheck( void );

void main( void )
{
   double n1, n2, r;
   int jmpret;
   /* Unmask all floating-point exceptions. */
    _control87( 0, _MCW_EM );
   /* Set up floating-point error handler. The compiler
    * will generate a warning because it expects
    * signal-handling functions to take only one argument.
    */
    if( signal( SIGFPE, fphandler ) == SIG_ERR )

   {
      fprintf( stderr, "Couldn't set SIGFPE\n" );
      abort();   }

   /* Save stack environment for return in case of error. First 
    * time through, jmpret is 0, so true conditional is executed. 
    * If an error occurs, jmpret will be set to -1 and false 
    * conditional will be executed.
    */
   jmpret = setjmp( mark );
   if( jmpret == 0 )
   {
      printf( "Test for invalid operation - " );
      printf( "enter two numbers: " );
      scanf( "%lf %lf", &n1, &n2 );
      r = n1 / n2;
      /* This won't be reached if error occurs. */
      printf( "\n\n%4.3g / %4.3g = %4.3g\n", n1, n2, r );

      r = n1 * n2;
      /* This won't be reached if error occurs. */
      printf( "\n\n%4.3g * %4.3g = %4.3g\n", n1, n2, r );
   }
   else
      fpcheck();
}
/* fphandler handles SIGFPE (floating-point error) interrupt. Note
 * that this prototype accepts two arguments and that the 
 * prototype for signal in the run-time library expects a signal 
 * handler to have only one argument.
 *
 * The second argument in this signal handler allows processing of
 * _FPE_INVALID, _FPE_OVERFLOW, _FPE_UNDERFLOW, and 
 * _FPE_ZERODIVIDE, all of which are Microsoft-specific symbols 
 * that augment the information provided by SIGFPE. The compiler 
 * will generate a warning, which is harmless and expected.

 */
void fphandler( int sig, int num )
{
   /* Set global for outside check since we don't want
    * to do I/O in the handler.
    */
   fperr = num;
   /* Initialize floating-point package. */
   _fpreset();
   /* Restore calling environment and jump back to setjmp. Return 
    * -1 so that setjmp will return false for conditional test.
    */
   longjmp( mark, -1 );
}
void fpcheck( void )
{
   char fpstr[30];
   switch( fperr )
   {
   case _FPE_INVALID:
       strcpy( fpstr, "Invalid number" );
       break;
   case _FPE_OVERFLOW:
       strcpy( fpstr, "Overflow" );

       break;
   case _FPE_UNDERFLOW:
       strcpy( fpstr, "Underflow" );
       break;
   case _FPE_ZERODIVIDE:
       strcpy( fpstr, "Divide by zero" );
       break;
   default:
       strcpy( fpstr, "Other floating point error" );
       break;
   }
   printf( "Error %d: %s\n", fperr, fpstr );
}


Output

Test for invalid operation - enter two numbers: 5 0
Error 131: Divide by zero


Floating-Point Support Routines

See Also   _exec Function Overview, signal, _spawn Function Overview, system
colinares 2016-11-23
  • 打赏
  • 举报
回复
引用 5 楼 fefe82 的回复:
[quote=引用 4 楼 colinares 的回复:] 我根据1楼的代码,做了一些测试,采用_control87来控制浮点数运算的异常处理模式来决定是否中断。 因为有些pc能中断提示,有些pc没有中断提示,那么是否可以怀疑不同pc上的系统库对于浮点数运算异常处理模式其实是不一样的? 在不修改系统代码和客户代码的情况下,更改相关操作系统库就可以解决这个问题?
不同 CPU 的处理也是不同的。[/quote] 也有怀疑是cpu的问题,所以直接更换pc,但出问题的pc还没到手上,现在还无法确定到底是操作系统原因还是pc硬件原因。 谢谢各位打下帮忙!
引用 5 楼 fefe82 的回复:
[quote=引用 4 楼 colinares 的回复:] 我根据1楼的代码,做了一些测试,采用_control87来控制浮点数运算的异常处理模式来决定是否中断。 因为有些pc能中断提示,有些pc没有中断提示,那么是否可以怀疑不同pc上的系统库对于浮点数运算异常处理模式其实是不一样的? 在不修改系统代码和客户代码的情况下,更改相关操作系统库就可以解决这个问题?
不同 CPU 的处理也是不同的。[/quote] 也有怀疑是cpu的问题,所以直接更换pc,但出问题的pc还没到手上,现在还无法确定到底是操作系统原因还是pc硬件原因。 谢谢各位帮忙!
fefe82 2016-11-23
  • 打赏
  • 举报
回复
引用 4 楼 colinares 的回复:
我根据1楼的代码,做了一些测试,采用_control87来控制浮点数运算的异常处理模式来决定是否中断。 因为有些pc能中断提示,有些pc没有中断提示,那么是否可以怀疑不同pc上的系统库对于浮点数运算异常处理模式其实是不一样的? 在不修改系统代码和客户代码的情况下,更改相关操作系统库就可以解决这个问题?
不同 CPU 的处理也是不同的。
colinares 2016-11-23
  • 打赏
  • 举报
回复
我根据1楼的代码,做了一些测试,采用_control87来控制浮点数运算的异常处理模式来决定是否中断。 因为有些pc能中断提示,有些pc没有中断提示,那么是否可以怀疑不同pc上的系统库对于浮点数运算异常处理模式其实是不一样的? 在不修改系统代码和客户代码的情况下,更改相关操作系统库就可以解决这个问题?
小灸舞 2016-11-23
  • 打赏
  • 举报
回复
因为C++标准眼里,除0错不是一个异常。再进一步:C++编译器,在编译除法操作时,没有为它加上额外的检查代码以抛出一个异常;也没有要求处理不同OS之间对(已经发生的)除0错的处理。
http://blog.csdn.net/nanyu/article/details/6475555

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧