今天我在我刚刚编写的一些代码中追踪一个浮点异常。找了一会才发现,因为其实是取整数模零导致的。显然做任何模零都不会被定义,但我认为错误如此具有误导性是很奇怪的。 C++ 模运算符中将 float 用于两个整数的是什么? (我使用的是 gcc 4.3.2)
下面是一个演示错误的简单程序。
int main()
{
int a=3,b=0;
int c=a%b;
return 0;
}
最佳答案
操作触发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) run
Starting program: /home/emil/float
Program received signal SIGFPE, Arithmetic exception.
0x0804837d in main () at float.c:4
4 int c=a%b;
关于c++ - 为什么当 b 为零时 a%b 产生 SIGFPE?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25631840/