/****************************************************************************** * FILE: opt01.c * DESCRIPTION: * Demonstrates small differences in results which should be identical, due * to non-association of floating point operations. ******************************************************************************/ #include #define SEED 1995 main() { double A, B, C; int i; srandom(SEED); for(i=0;i<5;i++) { A = ((float) (random() % 1000000)) / ((float) (random() % 1000000)); B = ((float) (random() % 1000000)) / ((float) (random() % 1000000)); C = ((float) (random() % 1000000)) / ((float) (random() % 1000000)); if (((A + B) + C) != (A + (B + C))) printf("%.15f %.15f\n", (A + B) + C, A + (B + C)); } }