Divide by zero - no floating point exception thrown

Hello Cameron,

From the thread you mentioned I can see why this behaviour is the way it is. Some documentation on this behaviour would help because natural expectation of people might be to see a floating point exception being thrown. I think this thread will show up on next web search though.

------- Original Message -------

Sender : Cameron McInallycameron.mcinally@nyu.edu

Title : Re: [cfe-dev] Divide by zero - no floating point exception thrown

Hello all,

I saw some odd behaviour in clang for the following test case. g++ and intel compiler produce binary which dumps “Floating point exception (core dumped)” on execution. Incase of clang random values are returned for each execution. Originally I had written a basic template specialization code which does a divide by zero and saw this bug. However clang warns of divide by zero similar to g++ and icc.

clang version is some trunk head revision on 12 sep 2013.


void F002() {
int b = 23/0;
std::cout << "\n b == " <<
b << std::endl;

int main() {

======= Following code also executes fine in clang without floating point exception. g++ & intel compiler binary emits floating point exception ======

template <int d, int e>
int F004() {
return d/e;

int main() {

============ clang version info ============

test$ clang++ -v sf2.cpp
clang version 3.4
Target: x86_64-unknown-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7.2
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7
“/usr/local/bin/clang-3.4” -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name sf2.cpp -mrelocation-model static -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -v -resource-dir /usr/local/bin/…/lib/clang/3.4 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/4.7/…/…/…/…/include/c++/4.7 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/4.7/…/…/…/…/include/c++/4.7/x86_64-linux-gnu -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/4.7/…/…/…/…/include/c++/4.7/backward -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/4.7/…/…/…/…/include/x86_64-linux-gnu/c++/4.7 -internal-isystem /usr/local/include -internal-isystem /usr/local/bin/…/lib/clang/3.4/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir /opt/SISO_BDP_SENTHIL_LLVM_JUL25/test -ferror-limit 19 -fmessage-length 135 -mstackrealign -fobjc-runtime=gcc -fobjc-default-synthesize-properties -fcxx-exceptions -fexceptions -fdiagnostics-show-option -vectorize-slp -o /tmp/sf2-0ecc11.o -x c++ sf2.cpp
clang -cc1 version 3.4 based upon LLVM 3.4svn default target x86_64-unknown-linux-gnu

Hey Senthil,

Maybe I can save you some trouble…


To sum it up, integer division by zero is undefined behaviour and the compiler is free to do what it would like to do. Although, I am still not thrilled with this behaviour in Clang/LLVM. My preferred handling of this undefined behaviour is to fall back to the processor’s expected behaviour. I believe that this is the “nice” thing to do.

Hope that helps,


Senthil Kumar