Try every possible denominator of
DENDIG
digits or fewer.
Compute the numerator as NUMER = NINT(X DENOM)
Keep the closest approximation.
Old-school boss should approve of a no-brain solution in
FORTRAN.
FUNCTION F2F (INPVAL, DENDIG)
IMPLICIT NONE
REAL INPVAL
INTEGER DENDIG
CHARACTER*32 F2F
INTEGER KTRIM, LTRIM
INTEGER NUMER, DENOM, BESTN, BESTD, MAXDEN, K, L, WHOLE
REAL AERR, BESTER, FRACT
CHARACTER*32 NUMSTR
MAXDEN = 10 **DENDIG - 1
WHOLE = INT(INPVAL)
FRACT = INPVAL - FLOAT(WHOLE)
BESTER = 1.0
BESTD = 1
BESTN = 0
DO 10 DENOM = MAXDEN, 2, -1
NUMER = NINT(FRACT * FLOAT(DENOM))
AERR = ABS(FRACT - FLOAT(NUMER) / FLOAT(DENOM))
IF (AERR .LE. BESTER) THEN
BESTER = AERR
BESTN = NUMER
BESTD = DENOM
END IF
10 CONTINUE
F2F = ''
20 FORMAT (I16)
IF (WHOLE > 0) THEN
WRITE (UNIT=NUMSTR, FMT=20) WHOLE
K = KTRIM(NUMSTR)
L = LTRIM(NUMSTR)
F2F = NUMSTR(K:L)
END IF
WRITE (UNIT=NUMSTR, FMT=20) BESTN
K = KTRIM(NUMSTR)
L = LTRIM(NUMSTR)
F2F = F2F(1:LTRIM(F2F)) // ' ' // NUMSTR(K:L)
WRITE (UNIT=NUMSTR, FMT=20) BESTD
K = KTRIM(NUMSTR)
L = LTRIM(NUMSTR)
F2F = F2F(1:LTRIM(F2F)) // '/' // NUMSTR(K:L)
RETURN
END
FUNCTION LTRIM (STRING)
IMPLICIT NONE
CHARACTER*(*) STRING
INTEGER LTRIM
INTEGER J, L
L = LEN(STRING)
DO 50 J = L, 1, -1
IF (STRING(J:J) .EQ. ' ') GO TO 50
LTRIM = J
RETURN
50 CONTINUE
LTRIM = 0
RETURN
END
FUNCTION KTRIM (STRING)
IMPLICIT NONE
CHARACTER*(*) STRING
INTEGER KTRIM
INTEGER J, L
L = LEN(STRING)
DO 50 J = 1, L, +1
IF (STRING(J:J) .EQ. ' ') GO TO 50
KTRIM = J
RETURN
50 CONTINUE
KTRIM = 0
RETURN
END
PROGRAM FPI
IMPLICIT NONE
INTEGER J
REAL PI
CHARACTER*32 F2F, STRING
PI = ACOS(-1.)
DO J = 1, 7
STRING = F2F(PI, J)
PRINT *, 'with ', J, ' digits, the best representation ',
$ 'of pi is ', STRING
END DO
STRING = F2F(.00205501, 6)
PRINT *, 'F2F(.00205501, 6): ', STRING
END
sample output:
with 1 digits, the best representation of pi is 3 1/7
with 2 digits, the best representation of pi is 3 14/99
with 3 digits, the best representation of pi is 3 16/113
with 4 digits, the best representation of pi is 3 16/113
with 5 digits, the best representation of pi is 3 13966/98635
with 6 digits, the best representation of pi is 3 132749/937541
with 7 digits, the best representation of pi is 3 593883/4194304
F2F(.00205501, 6): 1293/629194 ! closer approximation !