#include #include int test(double x) { double d = 1.0; double d0 = 0; unsigned char *c_x = (unsigned char *) &x; int nmatches = 0; int nnomatches = 0; int i; fprintf(stderr, "binary format: "); for (i = 7 ; i >= 0 ; i--) fprintf(stderr, "%s%02x", i < 7 ? " " : "", c_x[i]); fprintf(stderr, "\n"); fprintf(stderr, "x = %20.18f\n", x); while (d != d0) { double z = floor(d * 3); double z1 = z + 1.0; double y = d / z; double y1 = d / z1; /* Check if both sides of d * 3 doesn't make match */ if (y == x || y1 == x) nmatches++; else nnomatches++; d0 = d; d = d * 10; } fprintf(stderr, " %d matches, %d no_matches\n", nmatches, nnomatches); } int main(void) { test(0.3333333333333333); test(0.333333333333342); test(0.33333333333333342); return 0; }