Add NaN-propagation in coerceIn(ClosedComparableRange<Double>)

This commit is contained in:
Ilya Gorbunov
2016-12-08 01:40:52 +03:00
parent ea77673dde
commit 8ec2a2e6fe
3 changed files with 15 additions and 4 deletions
+4 -2
View File
@@ -871,8 +871,10 @@ public fun Double.coerceIn(minimumValue: Double, maximumValue: Double): Double {
public fun <T: Comparable<T>> T.coerceIn(range: ClosedComparableRange<T>): T {
if (range.isEmpty()) throw IllegalArgumentException("Cannot coerce value to an empty range: $range.")
return when {
!range.lessThanOrEquals(range.start, this) -> range.start
!range.lessThanOrEquals(this, range.endInclusive) -> range.endInclusive
// this < start equiv to this <= start && !(this >= start)
range.lessThanOrEquals(this, range.start) && !range.lessThanOrEquals(range.start, this) -> range.start
// this > end equiv to this >= end && !(this <= end)
range.lessThanOrEquals(range.endInclusive, this) && !range.lessThanOrEquals(this, range.endInclusive) -> range.endInclusive
else -> this
}
}