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 nonzero value corresponding to FE_DIVBYZERO
, but it prints 0. Changing the second line of main
to double x = 1.0/0.0;
gives the expected behavior. Is this permitted, or is it a bug?
Edit: For what it's worth, at first it may seem that in most real-world code, the operations which might cause fenv exceptions to be raised could not be optimized out, so one could safely perform large computations and check at the end whether any overflow, div-by-zero, etc. happened. However, things get messy and a real issue emerges when you consider inlining and optimization. If such a function got inlined in a situation where it would always end up dividing by zero due to constant arguments, gcc might get really smart and optimize the whole inlined function essentially to return INFINITY;
without raising any exceptions.