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

Content added Content deleted
(→‎{{header|REXX}}: added the REXX language. -- ~~~~)
(→‎{{header|REXX}}: aligned the output. -- ~~~~)
Line 203: Line 203:
Programming notes:
Programming notes:
<br><br>The &nbsp; '''numeric digits''' &nbsp; can be increased to a higher value to generate more terms.
<br><br>The &nbsp; '''numeric digits''' &nbsp; can be increased to a higher value to generate more terms.
<br>Two subroutines &nbsp; '''sqrt''' &nbsp; &amp; &nbsp; '''pi''' &nbsp; were included here to demonstrate terms for √2 and π.
<br>Two subroutines &nbsp; '''sqrt''' &nbsp; &amp; &nbsp; '''pi''' &nbsp; were included here to demonstrate terms for &nbsp; √2 &nbsp; and &nbsp; π.
<br>The subroutine &nbsp; '''$maxfact''' &nbsp; was included and is only needed if the number used for &nbsp; '''r2cf''' &nbsp; is a decimal fraction.
<br>The subroutine &nbsp; '''$maxfact''' &nbsp; was included and is only needed if the number used for &nbsp; '''r2cf''' &nbsp; is a decimal fraction.
<br>Checks were included to verify that the arguments being passed to &nbsp; '''r2cf''' &nbsp; are indeed numeric and also not zero.
<br>Checks were included to verify that the arguments being passed to &nbsp; '''r2cf''' &nbsp; are indeed numeric and also not zero.
Line 210: Line 210:
numeric digits 230 /*this determines how many terms */
numeric digits 230 /*this determines how many terms */
/*can be generated for dec fracts*/
/*can be generated for dec fracts*/
say ' 1/2 ──► CF: ' r2cf( '1/2' )
say ' 1/2 ──► CF: ' r2cf( '1/2' )
say ' 3 ──► CF: ' r2cf( 3 )
say ' 3 ──► CF: ' r2cf( 3 )
say '23/8 ──► CF: ' r2cf( '23/8' )
say ' 23/8 ──► CF: ' r2cf( '23/8' )
say '13/11 ──► CF: ' r2cf( '13/11' )
say ' 13/11 ──► CF: ' r2cf( '13/11' )
say '22/7 ──► CF: ' r2cf( '22/7 ' )
say ' 22/7 ──► CF: ' r2cf( '22/7 ' )
say
say
say '───────── attempts at √2.'
say '───────── attempts at √2.'
Line 238: Line 238:
g = $maxfact(g)
g = $maxfact(g)
end
end
if pos('/',g)==0 then g = g"/"1

if pos('/',g)==0 then g = g"/"1
parse var g n '/' d
parse var g n '/' d
if \datatype(n,'W') then call serr "a numerator isn't an integer:" n
if \datatype(n,'W') then call serr "a numerator isn't an integer:" n
Line 274: Line 273:
'''output'''
'''output'''
<pre style="overflow:scroll">
<pre style="overflow:scroll">
1/2 ──► CF: 0 2
1/2 ──► CF: 0 2
3 ──► CF: 3
3 ──► CF: 3
23/8 ──► CF: 2 1 7
23/8 ──► CF: 2 1 7
13/11 ──► CF: 1 5 2
13/11 ──► CF: 1 5 2
22/7 ──► CF: 3 7
22/7 ──► CF: 3 7


───────── attempts at √2.
───────── attempts at √2.