Introduce ClosedComparableRange interface which inherits ClosedRange to provide special comparison operation for double and float.

This commit is contained in:
Ilya Gorbunov
2016-11-25 07:14:14 +03:00
parent 5773594412
commit d60fc7d9a8
6 changed files with 71 additions and 25 deletions
@@ -12,7 +12,12 @@ class ComparablePair<T : Comparable<T>>(val first: T, val second: T) : Comparabl
fun <T : Comparable<T>> genericRangeTo(start: T, endInclusive: T) = start..endInclusive
operator fun Double.rangeTo(other: Double) = genericRangeTo(this, other)
operator fun Float.rangeTo(other: Float) = ClosedFloatRange(this, other)
// some weird inverted range
operator fun Float.rangeTo(other: Float) = object : ClosedComparableRange<Float> {
override val endInclusive: Float = this@rangeTo
override val start: Float = other
override fun lessThanOrEquals(a: Float, b: Float) = a >= b
}
fun check(x: Double, left: Double, right: Double): Boolean {
val result = x in left..right