From 3e69820907fc558837b4e851a337b0f12c5073b8 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 8 Dec 2016 01:47:38 +0300 Subject: [PATCH] Fix ClosedComparableRange.contains implementation, add docs. --- libraries/stdlib/src/kotlin/ranges/Ranges.kt | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/libraries/stdlib/src/kotlin/ranges/Ranges.kt b/libraries/stdlib/src/kotlin/ranges/Ranges.kt index 146772e9596..43cef5730f3 100644 --- a/libraries/stdlib/src/kotlin/ranges/Ranges.kt +++ b/libraries/stdlib/src/kotlin/ranges/Ranges.kt @@ -2,10 +2,21 @@ @file:kotlin.jvm.JvmName("RangesKt") package kotlin.ranges +/** + * Represents a range of values (for example, numbers or characters). + * Extends [ClosedRange] interface providing custom operation [lessThanOrEquals] for comparing values of range domain type. + * + * This interface is implemented by floating point ranges returned by [Float.rangeTo] and [Double.rangeTo] operators to + * achieve IEEE-754 comparison order instead of total order of floating point numbers. + */ +@SinceKotlin("1.1") public interface ClosedComparableRange> : ClosedRange { - override fun contains(value: T): Boolean = lessThanOrEquals(value, start) && lessThanOrEquals(value, endInclusive) + override fun contains(value: T): Boolean = lessThanOrEquals(start, value) && lessThanOrEquals(value, endInclusive) override fun isEmpty(): Boolean = !lessThanOrEquals(start, endInclusive) + /** + * Compares two values of range domain type and returns true if first is less than or equal to second. + */ fun lessThanOrEquals(a: T, b: T): Boolean } @@ -63,7 +74,6 @@ private class ClosedDoubleRange ( * A closed range of values of type `Float`. * * Numbers are compared with the ends of this range according to IEEE-754. - */ @JvmVersion private class ClosedFloatRange ( @@ -100,12 +110,16 @@ public operator fun > T.rangeTo(that: T): ClosedRange = Comp /** * Creates a range from this [Double] value to the specified [other] value. + * + * Numbers are compared with the ends of this range according to IEEE-754. */ @SinceKotlin("1.1") public operator fun Double.rangeTo(that: Double): ClosedComparableRange = ClosedDoubleRange(this, that) /** * Creates a range from this [Float] value to the specified [other] value. + * + * Numbers are compared with the ends of this range according to IEEE-754. */ @JvmVersion @SinceKotlin("1.1")