本文介绍了除以零不会引发SIGFPE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小程序执行浮点除法为零,所以我期望SIGFPE。

I have a small program performing floating-point division by zero, so I expect SIGFPE.

#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

void signal_handler (int signo) {
    if(signo == SIGFPE) {
      std::cout << "Caught FPE\n";
    }
}

int main (void) {
  signal(SIGFPE,(*signal_handler));

  double b = 1.0;
  double c = 0.0;
  double d = b/c;
  std::cout << "d = "<< d << std::endl;
  return 0;
}

其实,我有以下输出:

d = inf

gcc版本4.5.2(Ubuntu / Linaro 4.5.2-8ubuntu4)

gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)

在这种情况下,我应该怎么做来抛出SIGFPE?

What should I do to throw SIGFPE in this case? Which factors FP operation behaviour depend on (compiler flags/CPU type and so on)?

感谢

推荐答案

对于浮点数,您可以通过设置FPU控制字来更改此行为。查看此处

For floating point numbers you can change this behavior by setting up FPU control word. Take a look here

这篇关于除以零不会引发SIGFPE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!