Consider the following code:
#include <fenv.h> #include <stdio.h> int main() { #pragma STDC FENV_ACCESS ON 1.0/0.0; printf("%x\n", fetestexcept(FE_ALL_EXCEPT)); }
I would expect it to print a non-zero value corresponding to FE_DIVBYZERO , but it prints 0. Changing the second line from main to double x = 1.0/0.0; gives the expected behavior. Is this allowed, or is it a mistake?
Edit: For what it's worth, at first it might seem that in most real codes, operations that may cause fenv exceptions cannot be optimized, so it would be safe to do large calculations and check at the end if an overflow occurred, div zero etc. However, things get messy, and the real problem arises when you consider investing and optimizing. If such a function is embedded in a situation where it will always be divided by zero due to constant arguments, gcc can become really smart and optimize the entire built-in function essentially until return INFINITY; without any exceptions.
source share