为什么这是一个浮点异常? [英] Why is this a floating point exception?
问题描述
今天我正在跟踪一个浮点异常在一些我刚刚写的代码。它需要一段时间才能找到,因为它实际上是由一个整数mod零引起的。显然做任何模零是不会被定义,但我认为奇怪的是,错误是如此误导。在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;
这篇关于为什么这是一个浮点异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!