Continued fraction/Arithmetic/Construct from rational number: Difference between revisions

m
→‎{{header|REXX}}: changed extra digits and optimized the sqrt function, added a comment.
(→‎{{header|XPL0}}: Added zkl)
m (→‎{{header|REXX}}: changed extra digits and optimized the sqrt function, added a comment.)
Line 722:
=={{header|REXX}}==
Programming notes:
* &nbsp; Increase <code>numeric digits</code> to a higher value to generate more terms.
* &nbsp; Two subroutines, <code>sqrt</code> &amp; <code>pi</code>, were included here to demonstrate terms for √2√<span style="text-decoration: overline">2</span> and π.
* &nbsp; The subroutine <code>$maxfact</code> was included and is only needed if the number used for <code>r2cf</code> is a decimal fraction.
* &nbsp; Checks were included to verify that the arguments being passed to <code>r2cf</code> are indeed numeric and also not zero.
* &nbsp; This version also handles negative numbers.
<lang rexx>/*REXX pgm converts decimal or rational fraction to a continued fraction*/
numeric digits 230 /*this determines how many terms */
Line 778:
serr: say; say '***error!***'; say; say arg(1); say; exit
/*─────────────────────────────SQRT subroutine──────────────────────────*/
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits();numeric digits 11i=; m.=9
g=.sqrtGuess();numeric digits 9; numeric form; h=d+6; doif j=x<0 while p>9then do; m.jx=p-x; pi=p%2+1'i'; end
doparse k=j+5 tovalue format(x,2,1,,0) by'E0' -1; ifwith m.k>11 theng numeric'E' digits_ m.k; g=g*.5*(g+x/g); end'e'_%2
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
numeric digits d; return g/1
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
.sqrtGuess: if x<0 then call sqrtErr; numeric form; m.=11; p=d+d%4+2
numeric digits d; return (g/1)i /*make complex if X < 0.*/
parse value format(x,2,1,,0) 'E0' with g 'E' _ .; return g*.5'E'_%2
/*─────────────────────────────MAXFACT subroutine───────────────────────*/
$maxFact: procedure; parse arg x 1 _x,y; y=10**(digits()-1); b=0; h=1