'+----------------------------------------+ '| | '| C h a i n F r a c t i o n s | '| | '| Wilf Hey 2006 | '+----------------------------------------+ template$ = "##.########" do print "Please key a decimal fraction" input "more than 0, less than 10: ";InputNbr$ '+----------------------------+ '| validate the keyed number | '+----------------------------+ Valid = 0 if len(InputNbr$) > 0 then Valid = 1 for i = 1 to len(InputNbr$) x$ = mid$(InputNbr$, i, 1) if instr("0123456789.", x$) = 0 then Valid = 0 end if next '+----------------------------+ '| confirm its range | '+----------------------------+ a = instr(InputNbr$, ".") x$ = mid$(x$, a+1) if instr(x$, ".") > 0 then Valid = 0 end if end if if Valid = 1 then a = val(InputNbr$) if (a > 0) and (a < 10) then Valid = a exit do end if Valid = 0 end if print "ERROR" loop until Valid > 0 x$ = using(template$, Valid) a = val(x$) '+----------------------------+ '| abbreviate to 8 decimals | '+----------------------------+ if a <> Valid then print "Taken as ";x$ '+----------------------------+ '| generate Chain Fraction | '+----------------------------+ print Chain$ = "" fracobject = 1 / Valid do until fracobject < 0.0000001 'very small remainder object = 1 / fracobject intobject = int(object) Chain$ = Chain$ + str$(intobject) + " " fracobject = object - intobject if len(Chain$) > 50 then fracobject = 0 'force end loop i = 0 do i = i + 1 loop until word$(Chain$, i) = "" Chainlen = i - 1 '+----------------------------+ '| Expand chain at each size | '+----------------------------+ i = 0 do print i = i + 1 x$ = "(" '+----------------------------+ '| insert commas between | '| terms in the chain | '+----------------------------+ for j = 1 to i x$ = x$ + word$( Chain$, j) + "," next x$ = left$( x$, len(x$)-1) x$ = x$ + ")" '+----------------------------+ '| generate a two-digit | '| sequence number | '+----------------------------+ print "<";right$(str$(100+i), 2);"> ";x$ '+----------------------------+ '| evaluate the value of | '| the chain | '+----------------------------+ BaseVal = 0 FractionA = 0 : FractionB = 1 for j = i to 1 step -1 a = val(word$( Chain$, j)) BaseVal = BaseVal + a FractionA = FractionA + FractionB * a if j <> 1 then BaseVal = 1/BaseVal a = FractionA FractionA = FractionB FractionB = a end if next x$ = using(template$, BaseVal) x$ = trim$(x$) lenx = len(x$) '+----------------------------+ '| suppress trailing zeros | '+----------------------------+ do if right$(x$,1) <> "0" then exit do x$ = left$(x$, len(x$)-1) loop until lenx = 0 '+----------------------------+ '| suppress decimal point | '| if it isn't needed | '+----------------------------+ if right$(x$,1) = "." then x$ = left$(x$, len(x$)-1) end if x$ = left$(x$ + space$(lenx), lenx) print " ";x$; '+----------------------------+ '| calculate error magnitude | '+----------------------------+ x$ = using(template$,BaseVal - Valid) x$ = trim$(x$) if left$(x$, 1) <> "-" then x$ = "+" + x$ if x$ = "+0.00000000" then x$ = " " '+----------------------------+ '| if spot on, stop going for | '| more items in the chain | '+----------------------------+ '' if x$ = " " then '' print : exit do '' end if if x$ = " " then x$ = "0." '+----------------------------+ '| print the error | '| with trailing zero | '| suppression) | '+----------------------------+ lenx = len(x$) do if right$(x$,1) <> "0" then exit do x$ = left$(x$, len(x$)-1) loop until lenx = 0 'this will never happen if right$(x$,1) = "." then x$ = left$(x$, len(x$)-1) end if x$ = left$(x$ + space$(lenx), lenx) if val(x$) = 0 then print else print " error ";x$ end if print FractionA;" / ";FractionB ' retire if extremely close (insignificant error) if val(x$) = 0 then exit do loop until i = Chainlen stop