#include #include #include #include #include #include void main(int argc, char *argv[]) { int kcount=0; double x, h, m=0.5, p=0.0; long a=0, b=0, e, t=0, c=1; struct timeval tv; struct fd_set readfd; tv.tv_sec = 0; tv.tv_usec = 0; /* Initializations */ if (argc != 2) { printf("Usage: spfrcn [decimal]\n"); exit(1); } printf("\nHit return at any time to stop...\n"); x = atof(argv[1]); printf("Ok: %.16f\n", x); while (1) /* Main Loop */ { t++; m+=x; e=m; h=((double)e / (double)t); if (fabs(h-x) < fabs(p-x)) { p=h; a=e; b=t; printf("%ld / %ld : %.16f\n", a, b, p); } if (h==x) { exit(1); } kcount++; if (kcount==1000) { /* every 1000 loops check to see if kbhit */ kcount=0; FD_ZERO( &readfd ); FD_SET( 0, &readfd ); if (select(1, &readfd, NULL, NULL, &tv)) { exit(1); } } } /* End of while(1) loop */ }