// This code shows that floating-point arithmetic has limited precision. // Just as one-third can’t be represented exactly as a finite decimal // number, 0.1 can’t be represented exactly as a finite binary number. #include #include int main() { double a=0.1, b=0.2; if (a+b == 0.3) puts("Absolutely equal!"); else puts("Unequal--huh?"); // OK, fine, we can’t ask if they’re exactly the same. // How about if we ask if they’re close enough? double difference = fabs(a+b - 0.3); if (difference < 1e-12) puts("Close enough for engineering"); else puts("Not very close"); return 0; } // Why did I use the absolute value? // Why did I use fabs, rather than just abs?