/* * Speed Fraction * by: Charlton Harrison * Computes the best approximate fractions for a decimal */ #include #include #include #include /*---------------------------------------------*/ /* for kbhit() */ #include #include /* Function for kbhit() - the gcc way */ /* Only works for the key though... */ int kbhit() { struct timeval wait; int ret; fd_set readfd; FD_ZERO(&readfd); FD_SET(0, &readfd); wait.tv_sec = 0; /* Wait zero seconds */ wait.tv_usec = 0; ret = select(1, &readfd, NULL, NULL, &wait); return(ret); } /*---------------------------------------------*/ int main(int argc, char *argv[]) { int kcount=0; mpz_t lpa, lasta, lastb, a, b, c, e, t, tai, tbi, last10; mpf_t m, p, lastp, x, h, fe, ft, taf, tbf; mpf_set_default_prec(128); mpz_init(e); mpz_init(tai); mpz_init(tbi); mpz_init(last10); mpz_init(lpa); mpz_set_ui(lpa, 0); mpz_init(lasta); mpz_set_ui(lasta, 0); mpz_init(lastb); mpz_set_ui(lastb, 0); mpz_init(a); mpz_set_ui(a, 0); mpz_init(b); mpz_set_ui(b, 0); mpz_init(c); mpz_set_ui(c, 1); mpz_init(t); mpz_set_ui(t, 0); mpf_init(x); mpf_init(h); mpf_init(fe); mpf_init(ft); mpf_init(taf); mpf_init(tbf); mpf_init(m); mpf_set_d(m, 0.5); mpf_init(p); mpf_set_d(p, 0.0); mpf_init(lastp); mpf_set_d(lastp, 0.0); /* Initializations */ if (argc != 2) { printf("Usage: spfrcn \n"); exit(1); } printf("\nHit return at any time to stop...\n"); mpf_init_set_str(x, argv[1], 10); mpf_out_str(stdout, 10, 0, x); printf("\n"); while (1) { /* Main Loop */ mpz_add_ui(t, t, 1); mpf_add(m, m, x); mpz_set_f(e, m); mpf_set_z(fe, e); mpf_set_z(ft, t); mpf_div(h, fe, ft); mpf_sub(taf, h, x); mpf_sub(tbf, p, x); mpf_abs(taf, taf); mpf_abs(tbf, tbf); if ((mpf_cmp(taf, tbf)) < 0) { mpf_set(p, h); mpz_set(a, e); mpz_set(b, t); } if ((mpz_cmp(a, lpa)) != 0) { if ((mpf_cmp(h, x)) == 0) { mpz_out_str(stdout, 10, a); printf(" / "); mpz_out_str(stdout, 10, b); printf(" : "); mpf_out_str(stdout, 10, 0, p); printf("\n"); exit(1); } mpz_ui_pow_ui(last10, 10, (mpz_sizeinbase(e, 10) - 1)); mpz_tdiv_q(tai, e, last10); mpz_tdiv_q(tbi, a, last10); if ((mpz_cmp(tai, tbi)) != 0) { mpz_out_str(stdout, 10, a); printf(" / "); mpz_out_str(stdout, 10, b); printf(" : "); mpf_out_str(stdout, 10, 0, p); printf("\n"); mpz_set(lpa, a); } } kcount++; if (kcount==1000) { /* every 1000 loops check to see if kbhit */ kcount=0; if (kbhit()) { exit(1); } } } /* End of Main Loop */ }