Introduce ClosedComparableRange interface which inherits ClosedRange to provide special comparison operation for double and float.
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user