Task: Convert decimal number to rational



C#: using System;
using System.Text;

namespace RosettaDecimalToFraction
{
    public class Fraction
    {
        public Int64 Numerator;
        public Int64 Denominator;
        public Fraction(double f, Int64 MaximumDenominator = 4096)
        {
            
            
            Int64 a;
            var h = new Int64[3] { 0, 1, 0 };
            var k = new Int64[3] { 1, 0, 0 };
            Int64 x, d, n = 1;
            int i, neg = 0;

            if (MaximumDenominator <= 1)
            {
                Denominator = 1;
                Numerator = (Int64)f;
                return;
            }

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

            while (f != Math.Floor(f)) { n <<= 1; f *= 2; }
            d = (Int64)f;

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

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

                x = a;
                if (k[1] * a + k[0] >= MaximumDenominator)
                {
                    x = (MaximumDenominator - k[0]) / k[1];
                    if (x * 2 >= a || k[1] >= MaximumDenominator)
                        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];
            }
            Denominator = k[1];
            Numerator = neg != 0 ? -h[1] : h[1];
        }
        public override string ToString()
        {
            return string.Format("{0} / {1}", Numerator, Denominator);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.OutputEncoding = UTF8Encoding.UTF8;
            foreach (double d in new double[] { 0.9054054, 0.518518, 0.75, 0.4285714, 0.833333,
                0.90909, 3.14159265358979, 2.7182818284590451 })
            {
                var f = new Fraction(d, d >= 2 ? 65536 : 4096);
                Console.WriteLine("{0,20} → {1}", d, f);

            }
        }
    }
}








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