Jump to content

Map range: Difference between revisions

m (→‎Version 4: changed a comment to reflect that the (old) REXX version is no longer shown (it has been updated).)
Line 1,163:
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
<lang julia>maprange(s, a, b) = let a1 = minimum(a), a2 = maximum(a), b1 = minimum(b), b2 = maximum(b)
b1 + (s-a1) * (b2-b1) / (a2-a1)
end</lang>
By using <code>maximum</code> and <code>minimum</code> in our implementation, we can pass <code>maprange</code> Julia's built-in <code>Range</code> type to represent the ranges ''a'' and ''b'':
<pre>
julia> maprange(6, 0:10, -1:0)
-0.4
 
<lang julia>function maprange([0:10]s, 0:10a, -1:0b)
a₁, a₂ = minimum(a), maximum(a)
11-element Array{Float64,1}:
b₁, b₂ = minimum(b), maximum(b)
-1.0
return b₁ + (s - a₁) * (b₂ - b₁) / (a₂ - a₁)
-0.9
end
-0.8
-0.7
-0.6
-0.5
-0.4
-0.3
-0.2
-0.1
0.0
 
julia>@show maprange(0:106, 01:10, -1:0)
julia>@show maprange(60:10, 0:10, -1:0)</lang>
-1.0:0.1:0.0
 
</pre>
{{out}}
<pre>maprange(6, 1:10, -1:0) = -0.4444444444444444
maprange(0:10, 0:10, -1:0) = -1.0:0.1:0.0</pre>
 
=={{header|K}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.