Task: Convert decimal number to rational






PHP: function asRational($val, $tolerance = 1.e-6)
{
    if ($val == (int) $val) {

        return $val;
    }

    $h1=1;
    $h2=0;
    $k1=0;
    $k2=1;
    $b = 1 / $val;

    do {
        $b = 1 / $b;
        $a = floor($b);
        $aux = $h1;
        $h1 = $a * $h1 + $h2;
        $h2 = $aux;
        $aux = $k1;
        $k1 = $a * $k1 + $k2;
        $k2 = $aux;
        $b = $b - $a;
    } while (abs($val-$h1/$k1) > $val * $tolerance);

    return $h1.'/'.$k1;
}
 
echo asRational(1/5)."\n"; // "1/5"
echo asRational(1/4)."\n"; // "1/4"
echo asRational(1/3)."\n"; // "1/3"
echo asRational(5)."\n"; // "5"





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