#include #include // Compile with -O0 int main(int argc, char *argv[]) { int i; uint64_t n; float x = 1.3; double z; // This should be 1.3, right? printf("%f\n", x); // Nope! printf("%.15f\n", x); // Now let's compute 1.3*10 for (i = 1; i <= 9; ++i) x += 1.3; // We expect to get 13, but we don't if (x == 13.0) { printf("x == 13.0\n"); } else { printf("x != 13.0\n"); } printf("%.6f\n", x); printf("%.15f\n", x); // And we get a different answer if we multiply instead of add x = 1.3; x *= 10.0f; printf("%.15f\n", x); // Let's make a big number that we can represent // precisely with floating point n = (uint64_t)1<<63; x = (float)n; printf("%f\n", x); printf("%.15f\n", x); // This is also precise printf("%.15f\n", 0.25); // Small number plus a big number: the small number goes away! x += 0.25; printf("%f\n", x); printf("%.15f\n", x); // doubles are more precise, but still imprecise x = 0.1; z = 0.1; printf("%.30f\n", x); printf("%.30f\n", z); return 0; }