Division by zero does not throw SIGFPE

前端 未结 3 1011
醉话见心
醉话见心 2020-12-17 11:04

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

#include 
#include 
#include 

        
相关标签:
3条回答
  • 2020-12-17 11:13

    You only get a signal if you perform an integer division by zero. For floating point numbers division by zero is well defined.

    This is actually explained rather well in the Wikipedia article.

    0 讨论(0)
  • 2020-12-17 11:17

    You don't get a signal because the default behavior on most machines is to pollute your data with NaNs (not-a-number) and infinities. You have to enable floating point exceptions, and how you do that is machine specific. Look at the system header fenv.h, if you have one. The function fesettrapenable enables catching floating point exceptions on many machines.

    Unfortunately, there is no standard function to turn floating point exceptions handling on.

    0 讨论(0)
  • 2020-12-17 11:23

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

    0 讨论(0)
提交回复
热议问题