Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
16
Task/Map-range/Kotlin/map-range.kotlin
Normal file
16
Task/Map-range/Kotlin/map-range.kotlin
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
// 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 (range1.endInclusive == range1.start) throw IllegalArgumentException("first range cannot be single-valued")
|
||||
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))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue