Jump to content

Map range: Difference between revisions

→‎Tcl: Added implementation
("in succession" is an unnecessary constraint. Also, it's good to encourage exposition of additional language features related to solving the problem.)
(→‎Tcl: Added implementation)
Line 112:
9 maps to -0.1
10 maps to 0</lang>
 
=={{header|Tcl}}==
<lang tcl>package require Tcl 8.5
proc rangemap {rangeA rangeB value} {
lassign $rangeA a1 a2
lassign $rangeB b1 b2
expr {$b1 + ($value - $a1)*double($b2 - $b1)/($a2 - $a1)}
}</lang>
Demonstration (using a curried alias to bind the ranges mapped from and to):
<lang tcl>interp alias {} demomap {} rangemap {0 10} {-1 0}
for {set i 0} {$i <= 10} {incr i} {
puts [format "%2d -> %5.2f" $i [demomap $i]]
}</lang>
Output:
<pre>
0 -> -1.00
1 -> -0.90
2 -> -0.80
3 -> -0.70
4 -> -0.60
5 -> -0.50
6 -> -0.40
7 -> -0.30
8 -> -0.20
9 -> -0.10
10 -> 0.00
</pre>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.