Minkowski question-mark function: Difference between revisions

m
→‎{{header|REXX}}: increased the number of decimal digits (precision), made code simpler, optimized some code.
No edit summary
m (→‎{{header|REXX}}: increased the number of decimal digits (precision), made code simpler, optimized some code.)
Line 803:
{{trans|Phix}}
<lang rexx>/*REXX program uses the Minkowski question─mark function to convert a continued fraction*/
numeric digits 2040 /*use enough dec. digits for precision.*/
say fmt( mink( 0.5 * (1+sqrt(5) ) ) ) fmt( 5/3 )
say fmt( minkI(-5/9) ) fmt( (sqrt(13) - 7) / 6)
say fmt( mink( minkI(0.718281828) ) ) fmt( mink( minkI(.1213141516171819) ) )
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
floor: procedure; parse arg x; _t= trunc(x); return t - return _ - (x<0) * (x\=_t)
fmt: procedure: parse arg za; d= digits(); return right( format(za, , digits() d- 2, 0), digits() d+5)
/*──────────────────────────────────────────────────────────────────────────────────────*/
mink: procedure: parse arg x; p= x % 1; if x>1 | x<0 then return p + mink(x-p)
Line 825:
minkI: procedure; parse arg x; p= floor(x); if x>1 | x<0 then return p + minkI(x-p)
if x=1 | x=0 then return x
currcur= 0; count= 1; maxIter limit= 200; $= /*limit: max iterations*/
#= 1 /*#: is the count. */
 
do until count#==maxIterlimit | words($)==maxIterlimit; x= x + x /*a fast double x= x */ 2
if currcur==0 then if x<1 then count #= count# + 1
else do; $= $ count#; count#= 1; curr cur= 1; x= x-1; end
else if x>1 then do; count#= count# + 1; x= x-1; end
else do; $= $ count#; count#= 1; curr cur= 0; end
if x==floor(x) then do; then $= $ countdo; leave; $= $ #; leave; end
end /*until*/
z= words($)
 
#ret= words1 / word($, z)
ret do j=z for z by -1; / ret= word($, #j) + 1 / ret
do j=#end for # by -1; ret= word($, /*j) + 1 */ ret
end /*j*/
return 1 / ret
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 847 ⟶ 846:
{{out|output|text=&nbsp; when using the internal default inputs:}}
<pre>
1.66666666666666666666666666673007566392 1.66666666666666666666666666666666666667
1.666666666666666963 1.666666666666666667
-0.56574145408933511781346312208825067563 -0.56574145408933511781346312208825067562
-0.565741454089335118 -0.565741454089335118
0.71828182799999999999999999999999992890 0.12131415161718190000000000000000000833
0.718281828000000011 0.121314151617181900
</pre>