Monday, April 21, 2008

int division by zero

What output do you get if you get a division by zero in integer math in C?
$ cat int_div_by_zero.c 
#include <stdio.h>

int main()
{
printf("%d\n", 3 / 0);
return 0;
}
$ gcc int_div_by_zero.c
int_div_by_zero.c: In function 'main':
int_div_by_zero.c:5: warning: division by zero
$ ./a.out
Floating point exception
Kaboom! Nothing. This result immediately raises the question, "Is integer math division as slow as or slower than floating point division since it seems to be implemented in terms of it?"

(We get the same result with 0/0 and also even when we compile with -fno-trapping-math.)

No comments: