Minkowski question-mark function: Difference between revisions

Content added Content deleted
(J: it's not ieee 754 representation, it's the algorithm)
m (J: make it easier to experiment with longer continued fractions (though it's not clear that this length should match for minkowski and its inverse))
Line 469: Line 469:
Implementation:
Implementation:


<lang J>minkowski=: {{
<lang J>ITERCOUNT=: 52

minkowski=: {{
f=. 1|y
f=. 1|y
node=. *i.2 2 NB. node of Stern-Brocot tree
node=. *i.2 2 NB. node of Stern-Brocot tree
B=.''
B=.''
for.i.52 do.
for.i.ITERCOUNT do.
B=.B, b=. f>:%/t=. +/node
B=.B, b=. f>:%/t=. +/node
node=. t (1-b)} node
node=. t (1-b)} node
end.
end.
(<.y)+B+/ .*2^-1+i.52
(<.y)+B+/ .*2x^-1+i.ITERCOUNT
}}
}}


Line 485: Line 487:
cur=. 0 NB. 1 if generating "top" side of cf
cur=. 0 NB. 1 if generating "top" side of cf
cnt=. 1 NB. bits
cnt=. 1 NB. bits
for.i.52 do.
for.i.ITERCOUNT do.
if. f=<. f do.
if. f=<. f do.
cf=. cf,%cnt break.
cf=. cf,%cnt break.