Task: Convert decimal number to rational

C: #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdint.h>


void rat_approx(double f, int64_t md, int64_t *num, int64_t *denom)
{
	
	int64_t a, h[3] = { 0, 1, 0 }, k[3] = { 1, 0, 0 };
	int64_t x, d, n = 1;
	int i, neg = 0;

	if (md <= 1) { *denom = 1; *num = (int64_t) f; return; }

	if (f < 0) { neg = 1; f = -f; }

	while (f != floor(f)) { n <<= 1; f *= 2; }
	d = f;

	
	for (i = 0; i < 64; i++) {
		a = n ? d / n : 0;
		if (i && !a) break;

		x = d; d = n; n = x % n;

		x = a;
		if (k[1] * a + k[0] >= md) {
			x = (md - k[0]) / k[1];
			if (x * 2 >= a || k[1] >= md)
				i = 65;
			else
				break;
		}

		h[2] = x * h[1] + h[0]; h[0] = h[1]; h[1] = h[2];
		k[2] = x * k[1] + k[0]; k[0] = k[1]; k[1] = k[2];
	}
	*denom = k[1];
	*num = neg ? -h[1] : h[1];
}

int main()
{
	int i;
	int64_t d, n;
	double f;

	printf("f = %16.14f\n", f = 1.0/7);
	for (i = 1; i <= 20000000; i *= 16) {
		printf("denom <= %d: ", i);
		rat_approx(f, i, &n, &d);
		printf("%lld/%lld\n", n, d);
	}

	printf("\nf = %16.14f\n", f = atan2(1,1) * 4);
	for (i = 1; i <= 20000000; i *= 16) {
		printf("denom <= %d: ", i);
		rat_approx(f, i, &n, &d);
		printf("%lld/%lld\n", n, d);
	}

	return 0;
}










Fortran:       MODULE PQ	
       INTEGER MSG	
       CONTAINS		
        INTEGER FUNCTION GCD(I,J)	
         INTEGER I,J	
         INTEGER N,M,R	
          N = MAX(I,J)	
          M = MIN(I,J)	
    1     R = MOD(N,M)		
          IF (R.GT.0) THEN	
            N = M			
            M = R			
            IF (R .GT. 1) GO TO 1	
          END IF			
          GCD = M			
        END FUNCTION GCD	

        SUBROUTINE RATIONAL10(X)
         DOUBLE PRECISION X	
         DOUBLE PRECISION R	
         INTEGER P,Q		
         INTEGER F,WHACK	
         PARAMETER (WHACK = 10**8)	
          P = X*WHACK + 0.5	
          Q = WHACK		
          F = GCD(P,Q)		
          P = P/F		
          Q = Q/F		
          R = DBLE(P)/DBLE(Q)	
          WRITE (MSG,1) P,Q,X - R,WHACK	
    1     FORMAT ("x - ",I0,"/",I0,T28," = ",F18.14,
     1     " via multiplication by ",I0)
        END SUBROUTINE RATIONAL10	

        SUBROUTINE RATIONAL(X)	
         DOUBLE PRECISION X	
         DOUBLE PRECISION R,E,BEST	
         INTEGER P,Q		
         INTEGER TRY,F		
          P = 1 + X	
          Q = 1		
          BEST = X*6	
          DO TRY = 1,10000000	
            R = DBLE(P)/DBLE(Q)		
            E = X - R			
            IF (ABS(E) .LE. BEST) THEN	
              BEST = ABS(E)*0.125		
              F = GCD(P,Q)			
              IF (BEST.LT.0.1D0) WRITE (MSG,1) P/F,Q/F,E	
    1         FORMAT ("x - ",I0,"/",I0,T28," = ",F18.14)	
              IF (F.NE.1) WRITE (MSG,*) "Common factor
              IF (E.EQ.0) EXIT			
            END IF			
            IF (E.GT.0) THEN	
              P = P + CEILING(E*Q)	
            ELSE IF (E .LT. 0) THEN	
              Q = Q + 1			
            END IF		
          END DO		
        END SUBROUTINE RATIONAL	

        SUBROUTINE RATIONALISE(X,WOT)	
         DOUBLE PRECISION X	
         CHARACTER*(*) WOT	
          WRITE (MSG,*) X,WOT	
          CALL RATIONAL10(X)	
          CALL RATIONAL(X)	
          WRITE (MSG,*)		
        END SUBROUTINE RATIONALISE	
      END MODULE PQ	

      PROGRAM APPROX
      USE PQ
      DOUBLE PRECISION PI,E
      MSG = 6
      WRITE (MSG,*) "Rational numbers near to decimal values."
      WRITE (MSG,*)
      PI = 1		
      PI = 4*ATAN(PI)	
      E = DEXP(1.0D0)	
      CALL RATIONALISE(0.1D0,"1/10 Repeating in binary..")
      CALL RATIONALISE(3.14159D0,"Pi approx.")
      CALL RATIONALISE(PI,"Pi approximated better.")
      CALL RATIONALISE(E,"e: rational approximations aren't much use.")
      CALL RATIONALISE(10.15D0,"Exact in decimal, recurring in binary.")
      WRITE (MSG,*)
      WRITE (MSG,*) "Variations on 67/74"
      CALL RATIONALISE(0.9054D0,"67/74 = 0·9(054) repeating in base 10")
      CALL RATIONALISE(0.9054054D0,"Two repeats.")
      CALL RATIONALISE(0.9054054054D0,"Three repeats.")
      WRITE (MSG,*)
      WRITE (MSG,*) "Variations on 14/27"
      CALL RATIONALISE(0.518D0,"14/27 = 0·(518) repeating in decimal.")
      CALL RATIONALISE(0.519D0,"Rounded.")
      CALL RATIONALISE(0.518518D0,"Two repeats, truncated.")
      CALL RATIONALISE(0.518519D0,"Two repeats, rounded.")
      END