Why does a%b produce SIGFPE when b is zero?

后端 未结 2 1197
温柔的废话
温柔的废话 2020-11-29 08:08

Today I was tracking down a floating point exception in some code I had just written. It took a little while to find because it was actually caused by taking an integer mod

2条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 08:58

    The operation triggers SIGFPE:

    SIG is a common prefix for signal names; FPE is an acronym for floating-point exception. Although SIGFPE does not necessarily involve floating-point arithmetic, there is no way to change its name without breaking backward compatibility.

    GDB is a bit clearer about this and calls it "Arithmetic exception":

    (gdb) run
    Starting program: /home/emil/float
    
    Program received signal SIGFPE, Arithmetic exception.
    0x0804837d in main () at float.c:4
    4           int c=a%b;
    

提交回复
热议问题