Frn: Anders Tiberg <anders.tiberg@telia.com>
Till: <filearchive@ticalc.org>
mne: AFrac
Datum:  den 14 oktober, 2006


      
        AFrac, BFrac and CFrac written by Anders Tiberg.


 Reducing rational values the best way is to use continued fraction.
There is no need to insert numerator and denominator separatly. You
can either put a decimal number or an expression into a single value
memory.

 CFrac is a simpler form, you could say the cousin from the country
to continued fraction:

 
 CFrac
:Prompt X
:abs(X->Y             -> is STO
:1->D
:Repeat Y<.01
:D/Y->D
:fPart(1/Y->Y         use built in inverse function   
:End
:If 1>fPart(D
:iPart(D->D
:Disp DX:D


 This is the smallest and fastest program. The drawback is the preci-
sion, since it uses the decimalparts of the terms, it starts to give
faulty answers when numerator and denominator reaches 40- to 50 000.


 BFrac uses continued fraction the conventional way:

 
 BFrac
:Prompt X
:abs(X->Y
:fPart(Y->F
:0->I
:0->dimL L
:While I<21 and fPart(Y
:I+1->I
:1/fPart(Y->Y
:Y->L1(I
:1->D:0->N
:For(J,I,1,-1
:D->P
:N+DiPart(L1(J->D
:P->N
:End
:If abs(DF-N)<=E-9     (.000000001)    <= means smaller than or equal to
:Then
:If .7>fPart(Y
:0->Y
:End
:End
:Disp DX:D

 BFrac has a much better precision than CFrac, it should be safe for
values up to a million for numerator and denominator. It can give a
correct answer to an expression like 17/191+19/193+23/199. However
it's much slower,it runs through its For-loop 231 times during its
21 steps, in a "sweeping the stairs from below"-manner.
 AFrac uses continued fraction that fits its algorithm from above the
expression, counting downwards, only once for every new term, saving
time and bytes,with the same good precision as BFrac, wich makes it
useful as a subroutene in a program of your own.
 AFrac1 is a shorter and faster version of AFrac, with a simpler test
for verifying the correctness of the obtained fraction.  


            
               Questions and/or input, mailto: anders.tiberg@telia.com




 