Minkowski question-mark function: Difference between revisions
Content deleted Content added
m used an over/under style for the two possible continued fraction representations. |
|||
Line 263: | Line 263: | ||
-0.5657414540893351 -0.5657414540893352 |
-0.5657414540893351 -0.5657414540893352 |
||
0.7182818280000092 0.1213141516171819 |
0.7182818280000092 0.1213141516171819 |
||
</pre> |
|||
=={{header|Julia}}== |
|||
{{trans|FreeBasic}} |
|||
<lang julia>function minkowski(x, maxiter=151) |
|||
p = Int(floor(x)) |
|||
(x > 1 || x < 0) && return p + minkowski(x - p, maxiter) |
|||
q, r, s, m, n = 1, p + 1, 1, 0, 0 |
|||
d, y = 1.0, Float64(p) |
|||
while true |
|||
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 |
|||
y, p, q = y + d, m, n |
|||
end |
|||
end |
|||
return y + d |
|||
end |
|||
function minkowski_inv(x, maxiter=151) |
|||
p = Int(floor(x)) |
|||
(x > 1 || x < 0) && return p + minkowski_inv(x - p, maxiter) |
|||
(x == 1 || x == 0) && return x |
|||
contfrac = [0] |
|||
curr, coun, i = 0, 1, 0 |
|||
while true |
|||
x *= 2 |
|||
if curr == 0 |
|||
if x < 1 |
|||
coun += 1 |
|||
else |
|||
i += 1 |
|||
append!(contfrac, fill(0, i - length(contfrac) + 1)) |
|||
contfrac[i] = coun |
|||
coun = 1 |
|||
curr = 1 |
|||
x -= 1 |
|||
end |
|||
else |
|||
if x > 1 |
|||
coun += 1 |
|||
x -= 1 |
|||
else |
|||
i += 1 |
|||
append!(contfrac, fill(0, i - length(contfrac) + 1)) |
|||
contfrac[i] = coun |
|||
coun = 1 |
|||
curr = 0 |
|||
end |
|||
end |
|||
if x == Int(floor(x)) |
|||
contfrac[i + 1] = coun |
|||
break |
|||
end |
|||
i == maxiter && break |
|||
end |
|||
ret = 1.0 / contfrac[i + 1] |
|||
for j in i:-1:1 |
|||
ret = contfrac[j] + 1.0 / ret |
|||
end |
|||
return 1.0 / ret |
|||
end |
|||
println(" ", minkowski((1 + sqrt(5)) / 2), " ", 5 / 3) |
|||
println(minkowski_inv(-5/9), " ", (sqrt(13) - 7) / 6) |
|||
println(" ", minkowski(minkowski_inv(0.718281828)), " ", |
|||
minkowski_inv(minkowski(0.1213141516171819))) |
|||
</lang>{{out}} |
|||
<pre> |
|||
1.6666666666696983 1.6666666666666667 |
|||
-0.5657414540893351-0.5657414540893352 |
|||
0.7182818280000092 0.12131415161718191 |
|||
</pre> |
</pre> |
||