Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating point exceptions - gcc bug?

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.

like image 648
R.. GitHub STOP HELPING ICE Avatar asked Jun 13 '11 21:06

R.. GitHub STOP HELPING ICE


1 Answers

This is expected behaviour. gcc doesn't evaluate the expression, because it would have nothing to do with it afterwards.

If you compile with "-Wall", it warns you that the statement has no effect, and that it ignores the pragma statement.

GCC is not fully C99 compliant. For more information, see: http://gcc.gnu.org/c99status.html

For the issue of implementing this behaviour, see: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20785

like image 114
dougallj Avatar answered Oct 28 '22 08:10

dougallj