Minkowski question-mark function: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 518: | Line 518: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
{{trans|FreeBASIC}} |
|||
<syntaxhighlight lang="julia">function |
<syntaxhighlight lang="julia">function questionmark(x) |
||
p = |
y, p = fldmod(x, 1) |
||
q, d = 1 - p, .5 |
|||
while y + d > y |
|||
if p < q |
|||
q -= p |
|||
⚫ | |||
⚫ | |||
m = p + r |
|||
(m < 0 || p < 0) && break |
|||
n = q + s |
|||
n < 0 && break |
|||
if x < (m / n) |
|||
r, s = m, n |
|||
else |
else |
||
p -= q |
|||
⚫ | |||
end |
end |
||
⚫ | |||
end |
end |
||
y |
|||
end |
end |
||
function |
function questionmark_inv(x) |
||
y, bits = fldmod(x, 1) |
|||
⚫ | |||
(x > 1 || x < 0) && return p + minkowski_inv(x - p, maxiter) |
|||
( |
while (y + /(lo...)) != (y + /(hi...)) |
||
bit, bits = fldmod(2*bits, 1) |
|||
contfrac = [0] |
|||
⚫ | |||
⚫ | |||
while i < maxiter |
|||
x *= 2 |
|||
if curr == 0 |
|||
if x < 1 |
|||
coun += 1 |
|||
else |
|||
⚫ | |||
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 |
||
y + /((lo .+ hi)...) |
|||
for j in i:-1:1 |
|||
ret = contfrac[j] + 1.0 / ret |
|||
⚫ | |||
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) |
|||
⚫ | |||
</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> |
||