Map range: Difference between revisions

Added Kotlin
No edit summary
(Added Kotlin)
Line 1,168:
(9;-0.1)
(10;0.0))</lang>
 
=={{header|Kotlin}}==
<lang scala>// version 1.0.6
 
class FloatRange(override val start: Float, override val endInclusive: Float) : ClosedRange<Float>
 
fun mapRange(range1: FloatRange, range2: FloatRange, value: Float): Float {
if (value !in range1) throw IllegalArgumentException("value is not within the first range")
if (range2.start == range1.start) throw IllegalArgumentException("ranges can't start with the same value")
return range2.start + (value - range1.start) * (range2.endInclusive - range2.start) / (range1.endInclusive - range1.start)
}
fun main(args: Array<String>) {
for (i in 0..10) {
val mappedValue = mapRange(FloatRange(0.0f, 10.0f), FloatRange(-1.0f, 0.0f), i.toFloat())
println(String.format("%2d maps to %+4.2f", i, mappedValue))
}
}</lang>
 
{{out}}
<pre>
0 maps to -1.00
1 maps to -0.90
2 maps to -0.80
3 maps to -0.70
4 maps to -0.60
5 maps to -0.50
6 maps to -0.40
7 maps to -0.30
8 maps to -0.20
9 maps to -0.10
10 maps to +0.00
</pre>
 
=={{header|Lasso}}==
9,486

edits