
	PROGRAM PIFRAC

C  RATIONAL FRACTION APPROXIMATIONS

	IMPLICIT DOUBLE PRECISION (D)
 
C	DPI=2.D00*DASIN(1.D00)
C	DPI=DEXP(1.D00)
2	WRITE (*,'(A)') ' Rational fraction approximations: Choices:'
	WRITE (*,'(A)') '    1. Pi to some integer power < 10'
	WRITE (*,'(A)') '    2. e  to some integer power <10'
	WRITE (*,'(A)') '    3. Square root of an integer <100'
5	WRITE (*,'(A)') ' Choose by number (0 for exit): '
	READ (*,'(I1)') K
	IF (K.EQ.0) GOTO 110
	WRITE (*,'(A\)') 'What integer? '
	READ (*,'(I2)') L
	IF (K.EQ.1) THEN
	  DPI=(2.D00*DASIN(1.D00))**L
	ELSEIF (K.EQ.2) THEN
	  DPI=DEXP(DFLOAT(L))
	ELSEIF (K.EQ.3) THEN
	  DPI=DSQRT(DFLOAT(L))
	ELSE
	  GOTO 5
	ENDIF
	NTBL=JFIX(DPI)
	NBBL=1
	NTBG=NTBL+1
	NBBG=1
	NTOP=NTBL
	NBOT=NBBL
	DBEST=.999D00
C	DBEST=1.25D-03
C	NTOP=3
C	NBOT=1
C	NTBG=22
C	NBBG=7
C	NTBL=3
C	NBBL=1
C	NN=0
C	WRITE (*,'(A)') '      Rational fraction approximations to pi'
	WRITE (*,'(A)') ' '
	WRITE (*,'(A)') '   Numerator Denominator       Ratio '//
     &'                Error'
C	D=DFLOAT(3)
C	D1=D-DPI
C	WRITE (*,*) 3,1,D,D1
C	D=DFLOAT(22)/DFLOAT(7)
C	D1=D-DPI
C	WRITE (*,*) 22,7,D,D1
10	D=DFLOAT(NTOP)/DFLOAT(NBOT)
	DEL=D-DPI
	DELABS=DABS(DEL) 
	IF (DELABS.LT.DBEST) THEN
    		WRITE (*,*) NTOP,NBOT,D,DEL
		IF (DELABS.LT.2.D-18) GOTO 100
		DBEST=DELABS       
		IF (DEL.GT.0.D00) THEN
			NTBG=NTOP
			NBBG=NBOT     
    		ELSE
			NTBL=NTOP
			NBBL=NBOT
    		ENDIF            
  	ENDIF	
	IF (DEL.GT.0.D00) THEN      
		X=FLOAT(NTOP)+FLOAT(NTBL)
	        IF (X.GT.2.**32-1) GOTO 100
		NTOP=NTOP+NTBL
		NBOT=NBOT+NBBL
	ELSE
		X=FLOAT(NTOP)+FLOAT(NTBG)
		IF (X.GT.2.**31-1) GOTO 100
		NTOP=NTOP+NTBG
		NBOT=NBOT+NBBG
	ENDIF                 
	GOTO 10
100	WRITE (*,'(A)') ' '
   	GOTO 2
110	END
