Minkowski question-mark function: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 518: Line 518:


=={{header|Julia}}==
=={{header|Julia}}==

{{trans|FreeBASIC}}
<syntaxhighlight lang="julia">function minkowski(x)
<syntaxhighlight lang="julia">function questionmark(x)
p = Int(floor(x))
y, p = fldmod(x, 1)
(x > 1 || x < 0) && return p + minkowski(x)
q, d = 1 - p, .5
q, r, s, m, n = 1, p + 1, 1, 0, 0
while y + d > y
d, y = 1.0, Float64(p)
if p < q
while true
q -= p
d /= 2.0
y + d == y && break
m = p + r
(m < 0 || p < 0) && break
n = q + s
n < 0 && break
if x < (m / n)
r, s = m, n
else
else
y, p, q = y + d, m, n
p -= q
y += d
end
end
d /= 2
end
end
return y + d
y
end
end


function minkowski_inv(x, maxiter=151)
function questionmark_inv(x)
p = Int(floor(x))
y, bits = fldmod(x, 1)
lo, hi = [0, 1], [1, 1]
(x > 1 || x < 0) && return p + minkowski_inv(x - p, maxiter)
(x == 1 || x == 0) && return x
while (y + /(lo...)) != (y + /(hi...))
bit, bits = fldmod(2*bits, 1)
contfrac = [0]
bit > 0 ? (lo .+= hi) : (hi .+= lo)
curr, coun, i = 0, 1, 0
while i < maxiter
x *= 2
if curr == 0
if x < 1
coun += 1
else
i += 1
push!(contfrac, 0)
contfrac[i] = coun
coun = 1
curr = 1
x -= 1
end
else
if x > 1
coun += 1
x -= 1
else
i += 1
push!(contfrac, 0)
contfrac[i] = coun
coun = 1
curr = 0
end
end
if x == Int(floor(x))
contfrac[i + 1] = coun
break
end
end
end
ret = 1.0 / contfrac[i + 1]
y + /((lo .+ hi)...)
for j in i:-1:1
ret = contfrac[j] + 1.0 / ret
end
return 1.0 / ret
end
end


x, y = 0.718281828, 0.1213141516171819
println(" ", minkowski((1 + sqrt(5)) / 2), " ", 5 / 3)
for (a, b) ∈ [
println(minkowski_inv(-5/9), " ", (sqrt(13) - 7) / 6)
(5/3, questionmark((1 + √5)/2)),
println(" ", minkowski(minkowski_inv(0.718281828)), " ",
((√13-7)/6, questionmark_inv(-5/9)),
minkowski_inv(minkowski(0.1213141516171819)))
(x, questionmark_inv(questionmark(x))),
(y, questionmark(questionmark_inv(y)))]
println(a, a ≈ b ? " ≈ " : " != ", b)
end
</syntaxhighlight>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
1.6666666666666667 ≈ 1.666666666667894
1.6666666666696983 1.6666666666666667
-0.5657414540893352 ≈ -0.5657414540893351
-0.5657414540893351 -0.5657414540893352
0.718281828 ≈ 0.7182818279999971
0.7182818280000092 0.12131415161718191
0.1213141516171819 ≈ 0.12131415161718095
</pre>
</pre>