Minkowski question-mark function: Difference between revisions

Content added Content deleted
No edit summary
m (→‎{{header|REXX}}: increased the number of decimal digits (precision), made code simpler, optimized some code.)
Line 803: Line 803:
{{trans|Phix}}
{{trans|Phix}}
<lang rexx>/*REXX program uses the Minkowski question─mark function to convert a continued fraction*/
<lang rexx>/*REXX program uses the Minkowski question─mark function to convert a continued fraction*/
numeric digits 20 /*use enough dec. digits for precision.*/
numeric digits 40 /*use enough dec. digits for precision.*/
say fmt(mink(0.5*(1+sqrt(5)))) fmt(5/3 )
say fmt( mink( 0.5 * (1+sqrt(5) ) ) ) fmt( 5/3 )
say fmt(minkI(-5/9)) fmt((sqrt(13)-7)/6)
say fmt( minkI(-5/9) ) fmt( (sqrt(13) - 7) / 6)
say fmt(mink(minkI(0.718281828))) fmt(mink(minkI(.1213141516171819)))
say fmt( mink( minkI(0.718281828) ) ) fmt( mink( minkI(.1213141516171819) ) )
exit 0 /*stick a fork in it, we're all done. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
floor: procedure; parse arg x; _= trunc(x); return _ - (x<0) * (x\=_)
floor: procedure; parse arg x; t= trunc(x); return t - (x<0) * (x\=t)
fmt: procedure: parse arg z; return right( format(z, , digits() - 2, 0), digits() +5)
fmt: procedure: parse arg a; d= digits(); return right( format(a, , d-2, 0), d+5)
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
mink: procedure: parse arg x; p= x % 1; if x>1 | x<0 then return p + mink(x-p)
mink: procedure: parse arg x; p= x % 1; if x>1 | x<0 then return p + mink(x-p)
Line 825: Line 825:
minkI: procedure; parse arg x; p= floor(x); if x>1 | x<0 then return p + minkI(x-p)
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
if x=1 | x=0 then return x
curr= 0; count= 1; maxIter= 200; $=
cur= 0; limit= 200; $= /*limit: max iterations*/
#= 1 /*#: is the count. */

do until count==maxIter | words($)==maxIter; x= x + x /*a fast double*/
do until #==limit | words($)==limit; x= x * 2
if curr==0 then if x<1 then count= count + 1
if cur==0 then if x<1 then #= # + 1
else do; $= $ count; count= 1; curr= 1; x= x-1; end
else do; $= $ #; #= 1; cur= 1; x= x-1; end
else if x>1 then do; count= count + 1; x= x-1; end
else if x>1 then do; #= # + 1; x= x-1; end
else do; $= $ count; count= 1; curr= 0; end
else do; $= $ #; #= 1; cur= 0; end
if x==floor(x) then do; $= $ count; leave; end
if x==floor(x) then do; $= $ #; leave; end
end /*until*/
end /*until*/
z= words($)

#= words($)
ret= 1 / word($, z)
ret= 1 / word($, #)
do j=z for z by -1; ret= word($, j) + 1 / ret
do j=# for # by -1; ret= word($, j) + 1 / ret
end /*j*/
end /*j*/
return 1 / ret
return 1 / ret
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 847: Line 846:
{{out|output|text=&nbsp; when using the internal default inputs:}}
{{out|output|text=&nbsp; when using the internal default inputs:}}
<pre>
<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>
</pre>