当b为零时,为什么a%b会产生SIGFPE? [英] Why does a%b produce SIGFPE when b is zero?

查看:137
本文介绍了当b为零时,为什么a%b会产生SIGFPE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我正在跟踪刚刚编写的某些代码中的浮点异常。找到它花了一些时间,因为它实际上是由整数mod 0引起的。显然,将不会定义mod 0的任何操作,但我认为错误如此令人误解是很奇怪的。在C ++模运算符中,将浮点数用于两个整数是什么? (我正在使用gcc 4.3.2)

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 zero. Obviously doing anything mod zero is not going to be defined but I thought it was strange that the error was so misleading. What is it within the C++ modulo operator that would use floating point for two integers? (I'm using gcc 4.3.2)

这是一个简单的程序来演示错误。

Here's a simple program to demonstrate the error.

int main()
{
    int a=3,b=0;
    int c=a%b;
    return 0;
}


推荐答案

操作触发 SIGFPE


SIG是信号
名称的通用前缀; FPE是
浮点异常的首字母缩写。尽管
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对此更为清晰,称之为算术异常:

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;

这篇关于当b为零时,为什么a%b会产生SIGFPE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆