repeating expansion



Below is a batch file which works fine ( only GiveRatioType funtion not )
Comments are welcome

Adam

/*

Maxima CAS batch file


Converting ratio of integers ( decimal fraction < 1.0) to Repeating 
Decimal

a = the dividend (number being divided) = numerator of the ratio
b = the divisor (number doing the division) = denominator of the ratio

a<b


the real quotient (answer)

quotient = dividend/divisor= a/b

algorithm :

Long Division of positive integers to Decimal Places ( without remainder )

https://www.khanacademy.org/math/arithmetic/fractions/decimals_fractions/v/converting-a-fraction-to-a-repeating-decimal
http://www-personal.umich.edu/~yxl/486/W11/decimals-of-ratls-W11.pdf

L = a leftover is the number that rermains after each step of long 
division. It is used to begin the next step
D = decimal digit of the long division step

to check result use :
http://www.knowledgedoor.com/2/calculators/convert_a_ratio_of_integers.html

*/

remvalue(all);
kill(all);


/* functions */


/* give decimal digit D of the long division step */
GiveD(leftover,divisor) := ceiling(float((10*leftover - divisor)/divisor))$

/* Give leftover for next step of long division */
GiveL(leftover,DecimalDigit,divisor) := 10*leftover - DecimalDigit*divisor$


/* ratio in lowest terms
   not working now
*/
GiveRatioType(ratio):=
(
   [numerator:num(ratio), FactorsList],
   FactorsList:ifactors(numerator),
   for f in FactorsList do if (first(first(f))!=2 and first(first(f))!=5)
      then return("repeating decimal expansion")
      else return("finite decimal expansion")
)$

compile(all)$



/* input numbers*/
a:19 $
b:27 $


/* automatic change to lowest terms */
dRatio:a/b;
/* GiveRatioType(dRatio); */
a: num(dRatio);
b: denom(dRatio);


dFloat:0.0 $  /* quotient */
k:0$ /* current position of decimal digit */
LeftoverList:[]$ /* list of leftovers */

/* Long Division of positive integers to Decimal Places ( without 
remainder ) */

/* first step */
L:a$
D:GiveD(L, b)$
k:k-1$
dFloat:dFloat +D*10^k$ /* save new digit to output float */
LeftoverList:cons(L,LeftoverList)$
disp(L,D)$


/* nest steps */

L:GiveL(L, D, b)$
while (not member(L,LeftoverList))  /* test for the end of repeating 
sequence */
do
  (
   D:GiveD(L, b),
   k:k-1,
   dFloat:dFloat +D*10^k, /* save new digit to output float */
   LeftoverList:cons(L,LeftoverList),
   disp(L,D),
   L:GiveL(L, D, b)
  )$


disp(dFloat)$