/* compare.c */ /* author: Weiwei Chen */ /* date: Oct. 11. 2010 */ #include int main(void) { /* variable definitions */ float x = 0, y = 0; float r = 0, R = 0; float pi = 3.14159; float epsilon = 1e-5; float diff; /* value assigment */ r = 3.2; R = 4.2; /* mathematically, the value of x should be equal to the value of y*/ x = pi * (R * R - r * r); y = pi * R * R - pi * r * r; diff = x - y; /* however, since the value in computer is approximate */ if(x == y) /* this condition is false */ { printf("x == y \n"); } if(x != y) /* this condition will be true */ { printf("x != y \n"); } /* how to get the correct answer with the consideration of approximation */ if(diff > -epsilon && diff < epsilon) { printf("approximately in computer, x == y \n"); } if(!(diff > -epsilon && diff < epsilon)) { printf("approximately in computer, x != y \n"); } return 0; }