Map range: Difference between revisions

no edit summary
(Go solution)
No edit summary
Line 516:
end
</lang>
 
=={{header|Ruby}}==
<lang ruby>def map_range(a, b, s)
(a1,a2), (b1, b2) = a, b
b1 + ((s-a1)*(b2-b1)/(a2-a1))
end
 
11.times do |s|
print s, " maps to ", map_range([0,10], [-1,0], s), "\n"</lang>
Output:
<pre>
0 maps to -1.0
1 maps to -0.9
2 maps to -0.8
3 maps to -0.7
4 maps to -0.6
5 maps to -0.5
6 maps to -0.4
7 maps to -0.30000000000000004
8 maps to -0.19999999999999996
9 maps to -0.09999999999999998
10 maps to 0.0
</pre>
 
=={{header|Tcl}}==
Anonymous user