I'm repeating the same calculation twice, but in one I get a floating point exception while in the other I don't.
#include <iostream>
#include <cmath>
#include <fenv.h>
using namespace std;
int main(void)
{
feenableexcept(-1);
double x,y,z;
x = 1.0;
y = (1.0/(24.3*24.0*3600.0))*x;
cout << "y = " << y << endl;
z = x/(24.3*24.0*3600.0);
cout << "z = " << z << endl;
return 0;
}
I tested it on both g++ and clang++ and got the following output in both
y = 4.76299e-07
Floating point exception
What's going on?
The problem is with
feenableexcept(-1);
This sets of FPE exceptions for all possible cases, include inexact results (which are very common in floating point operations). You really shouldn't be using a number here but instead use the provided macros to set the bits you want.
Replacing by
feenableexcept(FE_INVALID |
FE_DIVBYZERO |
FE_OVERFLOW |
FE_UNDERFLOW);
resolves the problem for me.
When
feenableexcept(FE_INVALID |
FE_DIVBYZERO |
FE_OVERFLOW |
FE_UNDERFLOW |
FE_INEXACT);
is given, the SIGFPE will return. This shows that FE_INEXACT is the root cause of the problem.
The reason that the first calculation is not giving SIGFPE is that the division is already done at compile time (with inexact results). At runtime only multiplication is performed, which does not introduce additional inexactness.