Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
Announcements
The Intel sign-in experience has changed to support enhanced security controls. If you sign in, click here for more information.
7779 Discussions

Division by zero inside if(0) clause

Bakhvalov__Pavel
Beginner
260 Views

Hello!

I encounter the strange behavior of the Intel C++ Compiler. 

The following program generates "Floating point exception" if optimization (at least -O1) is enabled:

#include <fenv.h>
#include <stdio.h>
int j = 0;
int main(int argc, char *argv[]){
    feenableexcept( FE_DIVBYZERO );
    double r = 0.0;
    for(int k=0; k<2; k++) {
        double A[3] = {0.,0.,0.};
        if(j) A[0] -= 1.0/r;
        printf("A = %e %e %e\n", A[0], A[1], A[2]);
    }    
}

Linux, 64bit. Compiler version: icc (ICC) 16.0.1 20151021.

If the optimization is disabled, no exception is generated. gcc also generates no exception.

Is everything correct?

0 Kudos
2 Replies
Viet_H_Intel
Moderator
260 Views

Hi,

I'll look into it further. In the mean time, can you use -fp-model strict?

Thanks,

Viet

Bakhvalov__Pavel
Beginner
260 Views

When compiled with option -fp-model strict, the result is as it should be:
A = 0.000000e+00 0.000000e+00 0.000000e+00
A = 0.000000e+00 0.000000e+00 0.000000e+00

Reply