/****************************************************************************** * FILE: opt02.c * DESCRIPTION: * Example program that illustrates how certain optimizations can effect * program correctness. In the nested loop below, if the j loop is the * innermost loop, then the negative exponents on the data will accumulate * until x is rounded off to zero. If the i loop is the innermost loop, * then the exponent of x never becomes so small as to be rounded off to * zero. ******************************************************************************/ #include double a[4][4] = {3.234e-80, 3.234e-70, 3.234e-60, 3.234e-50, 3.234e-70, 3.234e-60, 3.234e-50, 3.234e-40, 3.234e70, 3.234e60, 3.234e50, 3.234e40, 3.234e80, 3.234e70, 3.234e60, 3.234e50}; main() { double x; int i, j; x = 1.0; for(i=0;i<4;i++) for(j=0;j<4;j++) x = x * a[i][j]; printf("Result of the original loop is %f\n", x); /*****************************************************************************/ /* Interchange the next two loops to witness a vast change in x */ /*****************************************************************************/ x = 1.0; for(j=0;j<4;j++) for(i=0;i<4;i++) x = x * a[i][j]; printf("Result of the interchanged loop is %f\n", x); }