/* This program demonstrates fun with roundoff error. What I do is use some general code to calculate the angle of point (x,x) for x = 1, 2, 3, ..., 9. You'll note that this angle should be the same. When I calculate it in a reasonable way, the angle differs from point to point. I'm calculating it by calculating the hypotenuse of the triangle (0,0), (x,0), (x,x). I then divide x by this hypotenuse and call acos(). As you can see below, these values do not match for successive values of x. */ #include #include #include int main() { double x; double hyp; double angle; double lastangle; lastangle = 0; for (x = 1; x < 10; x++) { hyp = sqrt(x*x + x*x); angle = acos(x/hyp); if (x == 1) { printf("Point: (%lg,%lg). Angle: %lf\n", x, x, angle); } else { printf("Point: (%lg,%lg). Angle: %lf. Equal to last angle? %s\n", x, x, angle, (angle == lastangle) ? "true" : "false"); } lastangle = angle; } return 0; }