FRACTIONS

Most computer languages, including BASIC, provide functions and operations involving integers such as 127, or floating-point numbers such as 12.73, or both. However, on some occasions we may wish to perform calculations involving fractions, such as:

1/2 + 1/3

with the results given as an exact fraction, such as 5/6, rather than the less obvious, and less accurate, decimal 0.8333333.
The following program will take a calculation involving fractions, and give an exact fractional result (where possible). For example:

EVALUATE: 1/4 + 5 * 7/68 
1/4 + 5 * 7/68 = 13/17

The result can be given either in improper form, such as 7/6. or proper form, such as 1+1/6. The program detects if the result cannot be expressed as a fraction with sufficient accuracy, as shown in the following two examples:

EVALUATE: 3.14159292 3.14159292 = 355/113
EVALUATE: PI
PI = IRRATIONAL

The program would be useful for mathematical analysis, or for teaching the concept of fractions to children.


Description

The program evaluates the INPUT line using floating-point arithmetic, and converts the result from a floating-point number into a fraction. The method involves the construction of what is called a 'continued fraction', by repeating the following two simple steps until an integer is obtained:

  1. Take the reciprocal (i.e. one divided by the number)
  2. Subtract the integer part.

This can be illustrated by the following example, which converts 0.764705882 into a continued fraction:

x                   = 0.764705882
1/x                 = 1.30769231
1/x – 1        	    = 0.307692307
1/(1/x – 1)	    = 3.25
1/(1/x – 1) – 3     = 0.25
1/(1/(1/x – 1) – 3) = 4

Finally, solving the equation for x in the last line gives the exact answer that x = 13/17.


BBC Computer Version

In this version an integer variable, I%, is used to calculate the integer part of the number at each stage to avoid having to use the INT function. The remaining variables are left as floating-point variables for brevity. An "ON ERROR" statement detects syntax errors when the input equation is evaluated using EVAL.

1 REM ... Fractions ...

Set up error return for errors other than escape

10 ON ERROR IF ERR<>17 PRINT"WHAT?":GOTO60 ELSE END 
20 0%=1

Select whether fraction is to be displayed in proper or improper form.

30 REPEAT INPUT'"PROPER (P) OR IMPROPER (I)?" P$
40 UNTIL P$="P" OR P$="I"
60 REPEAT E=9E-8
70 INPUT "EVALUATE:"EQ$: PRINT EQ$;" = ";
80 S$="+": X=EVAL(EQ$) IF X<0 S$="-"
90 X=ABS X: IS=X: R=X-I%

Multiply accuracy by integer part. If fractional part negligible, treat as integer. If no integer part, more error tolerated.

95 IF I%<>0 THEN E=X*E 
100 IF R<=E GOTO 110
101 IF I%=0 THEN E=E*R 
102 IF ABS(R-1)>K GOT0120
104 I%=I%+1
110 IF S$="-" PRINT S$;
112 PRINT I%: UNTIL FALSE

Now do fractional art.

120 IF SS="-"" PRINT S$;
130 IF I%<>0 AND P$="P": PRINT I%,S$;: I%=0: X=R
190 K=1:L=1:M=0:J=I%

Work out continued fraction for R.

200 REPEAT R=l/R: I%=R
204 R=R-I%
210 N=J: J=J*I%+K: K=N 
220 N=L: L=L*I%+M: M=N 
230 UNTIL E>=ABS(X-J/L)

Test whether irrational. Choose 0.0033 so that PI comes out as irrational, but 355/113 is real.

240 IF ABS(J*L)*ABS(X-J/L)/X >0.0033 PRINT"IRRATIONAL":UNTIL FALSE
250 PRINT J"/"L: UNTIL FALSE

Variables:

I% - Integer part of %X
J - Numerator of fractional representation
L - Denominator of fractional representation
P$ - Proper "P" or improper "I" flag
R - Real part of %X
S$ - Sign of SX
X - Absolute value of rational

Atom Version

The Atom does not have an EVAL function, but fortunately the FINPUT statement in Atom BASIC will accept an expression in the input line. The input line is entered at address 320, so PRINT $320 in line 70 will echo the line.

1 REM .. FRACTIONS .. 
10 DIM P(1),S(1),E(100)

Set up line to be executed on an error.

20 @=0;?16=E;?17=E/256;$E="PRINT ""WHAT?""';G.s"

Select whether fraction to be displayed in proper or improper form.

30 DO INPUT'"PROPER (P) OR IMPROPER (I)"$P
40 UNTIL $P="P" OR $P="I"
50sDO %E=9E-8
70 PRINT"EVALUATE"; FINPUT %X; PRINT $320" = "
80 $S="+";FIF%X<0 $S="-"
90 %X=ABS%X;I=%X;SR=%X-I

Multiply accuracy by integer part. If fractional part negligible, treat as integer. If no integer part, more error tolerated.

95 IF I<>0 THEN %E=%X*%E 100 FIF %R<=%E GOTO i
101 IF I=0 THEN %E=%E*%R 102 FIF ABS(%R-1)>%E GOTO r 104 I=I+1
110iIF $S="-"PRINT $S
112 PRINT I;UNTIL 0

Now do fractional part.

120rIF $S="-" PRINT $S
130 IF I<>0 AND $P="P" PRINT I,$S; I=0; %X=SR
180 $E="PRINT ""IRRATIONAL""'; GOTO s"
190 K=1;L=1;M=0;J=I

Work out continued fraction for SR.

200 DO %R=1/%R; I=%R
204 %R=%R-I
210 N=J; J=J*I+K; K=N
220 N=L; L=L*I+M; M=N
230 FUNTIL %E>=ABS(%X-J/L)

Test whether irrational. Choose 0.033 so that PI comes out as irrational, but 355/113 is real.

240 FIF ABS(J*L)*ABS(%X-J/L)/%X >0.0033 PRINT "IRRATIONAL"; UNTIL 0
250 PRINT J"/"L;UNTIL 0

Variables:

I - Integer part of %X
J - Numerator of fractional representation
L - Denominator of fractional representation
$P - Proper "P" or improper "I" flag
%R - Real part of %X
$S - Sign of %X
%X - Absolute value of rational