From 1cf4a407d1d3acbec051623399b9a2a2743a4335 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Fri, 22 May 2015 19:54:04 +0300 Subject: [PATCH] JS: Provide MIN_VALUE and MAX_VALUE member constants for Double and Float companion objects. JVM: Make MIN_VALUE and MAX_VALUE not an extension but member constant properties of Double and Float companion objects. --- compiler/testData/builtin-classes.txt | 12 ++++++ .../src/kotlin/FloatingPointConstants.kt | 10 +++++ .../jvm/internal/PrimitiveCompanionObjects.kt | 4 ++ .../src/internal/primitiveCompanionObjects.kt | 4 ++ .../src/kotlin/jvm/BuiltinExtensions.kt | 35 ------------------ libraries/stdlib/test/numbers/NumbersTest.kt | 37 ++++++++++++++----- 6 files changed, 58 insertions(+), 44 deletions(-) delete mode 100644 libraries/stdlib/src/kotlin/jvm/BuiltinExtensions.kt diff --git a/compiler/testData/builtin-classes.txt b/compiler/testData/builtin-classes.txt index 6d9c6fa1234..de962a23b15 100644 --- a/compiler/testData/builtin-classes.txt +++ b/compiler/testData/builtin-classes.txt @@ -391,6 +391,10 @@ public final class Double : kotlin.Number, kotlin.Comparable { public companion object Companion : kotlin.FloatingPointConstants { /*primary*/ private constructor Companion() + public abstract override /*1*/ /*fake_override*/ val MAX_VALUE: kotlin.Double + public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Double + public abstract override /*1*/ /*fake_override*/ val MIN_VALUE: kotlin.Double + public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Double public abstract override /*1*/ /*fake_override*/ val NEGATIVE_INFINITY: kotlin.Double public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Double public abstract override /*1*/ /*fake_override*/ val NaN: kotlin.Double @@ -629,6 +633,10 @@ public final class Float : kotlin.Number, kotlin.Comparable { public companion object Companion : kotlin.FloatingPointConstants { /*primary*/ private constructor Companion() + public abstract override /*1*/ /*fake_override*/ val MAX_VALUE: kotlin.Float + public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Float + public abstract override /*1*/ /*fake_override*/ val MIN_VALUE: kotlin.Float + public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Float public abstract override /*1*/ /*fake_override*/ val NEGATIVE_INFINITY: kotlin.Float public abstract override /*1*/ /*fake_override*/ fun (): kotlin.Float public abstract override /*1*/ /*fake_override*/ val NaN: kotlin.Float @@ -700,6 +708,10 @@ public final class FloatRange : kotlin.Range, kotlin.Progression { + public abstract val MAX_VALUE: T + public abstract fun (): T + public abstract val MIN_VALUE: T + public abstract fun (): T public abstract val NEGATIVE_INFINITY: T public abstract fun (): T public abstract val NaN: T diff --git a/core/builtins/src/kotlin/FloatingPointConstants.kt b/core/builtins/src/kotlin/FloatingPointConstants.kt index 4572413d7d9..ac34a30cbfa 100644 --- a/core/builtins/src/kotlin/FloatingPointConstants.kt +++ b/core/builtins/src/kotlin/FloatingPointConstants.kt @@ -20,6 +20,16 @@ package kotlin * An interface implemented by companion objects of floating-point types. */ public interface FloatingPointConstants { + /** + * A constant holding the smallest *positive* nonzero value. + */ + public val MIN_VALUE: T + + /** + * A constant holding the largest positive finite value. + */ + public val MAX_VALUE: T + /** * A constant holding the positive infinity value. */ diff --git a/core/runtime.jvm/src/kotlin/jvm/internal/PrimitiveCompanionObjects.kt b/core/runtime.jvm/src/kotlin/jvm/internal/PrimitiveCompanionObjects.kt index c172b8c8665..c7f83a76886 100644 --- a/core/runtime.jvm/src/kotlin/jvm/internal/PrimitiveCompanionObjects.kt +++ b/core/runtime.jvm/src/kotlin/jvm/internal/PrimitiveCompanionObjects.kt @@ -17,12 +17,16 @@ package kotlin.jvm.internal private object DoubleCompanionObject : FloatingPointConstants { + override val MIN_VALUE: Double = java.lang.Double.MIN_VALUE + override val MAX_VALUE: Double = java.lang.Double.MAX_VALUE override val POSITIVE_INFINITY : Double = java.lang.Double.POSITIVE_INFINITY override val NEGATIVE_INFINITY : Double = java.lang.Double.NEGATIVE_INFINITY override val NaN : Double = java.lang.Double.NaN } private object FloatCompanionObject : FloatingPointConstants { + override val MIN_VALUE: Float = java.lang.Float.MIN_VALUE + override val MAX_VALUE: Float = java.lang.Float.MAX_VALUE override val POSITIVE_INFINITY : Float = java.lang.Float.POSITIVE_INFINITY override val NEGATIVE_INFINITY : Float = java.lang.Float.NEGATIVE_INFINITY override val NaN : Float = java.lang.Float.NaN diff --git a/js/js.libraries/src/internal/primitiveCompanionObjects.kt b/js/js.libraries/src/internal/primitiveCompanionObjects.kt index 4d9ab889234..553ba798799 100644 --- a/js/js.libraries/src/internal/primitiveCompanionObjects.kt +++ b/js/js.libraries/src/internal/primitiveCompanionObjects.kt @@ -17,12 +17,16 @@ package kotlin.js.internal private object DoubleCompanionObject : FloatingPointConstants { + override val MIN_VALUE: Double = js("Number.MIN_VALUE") + override val MAX_VALUE: Double = js("Number.MAX_VALUE") override val POSITIVE_INFINITY: Double = js("Number.POSITIVE_INFINITY") override val NEGATIVE_INFINITY: Double = js("Number.NEGATIVE_INFINITY") override val NaN: Double = js("Number.NaN") } private object FloatCompanionObject : FloatingPointConstants { + override val MIN_VALUE: Float = js("Number.MIN_VALUE") + override val MAX_VALUE: Float = js("Number.MAX_VALUE") override val POSITIVE_INFINITY : Float = js("Number.POSITIVE_INFINITY") override val NEGATIVE_INFINITY : Float = js("Number.NEGATIVE_INFINITY") override val NaN : Float = js("Number.NaN") diff --git a/libraries/stdlib/src/kotlin/jvm/BuiltinExtensions.kt b/libraries/stdlib/src/kotlin/jvm/BuiltinExtensions.kt deleted file mode 100644 index f7306e437e9..00000000000 --- a/libraries/stdlib/src/kotlin/jvm/BuiltinExtensions.kt +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package kotlin.jvm - - -/** - * A constant holding the smallest positive nonzero value of type `Double`, 2^-1074. - */ -public val Double.Companion.MIN_VALUE: Double get() = java.lang.Double.MIN_VALUE -/** - * A constant holding the largest positive finite value of type `Double`, (2-2^-52)*2^1023. - */ -public val Double.Companion.MAX_VALUE: Double get() = java.lang.Double.MAX_VALUE - -/** - * A constant holding the smallest positive nonzero value of type `Float`, 2^-149. - */ -public val Float.Companion.MIN_VALUE: Float get() = java.lang.Float.MIN_VALUE -/** - * * A constant holding the largest positive finite value of type `Float`, (2-2^-23)*2^127. - */ -public val Float.Companion.MAX_VALUE: Float get() = java.lang.Float.MAX_VALUE diff --git a/libraries/stdlib/test/numbers/NumbersTest.kt b/libraries/stdlib/test/numbers/NumbersTest.kt index 579cd6cf832..c3d4eb51138 100644 --- a/libraries/stdlib/test/numbers/NumbersTest.kt +++ b/libraries/stdlib/test/numbers/NumbersTest.kt @@ -12,35 +12,54 @@ class NumbersTest { // overflow behavior // doesn't hold for JS Number - // assertEquals(Int.MIN_VALUE, Int.MAX_VALUE + 1) + // expect(Int.MIN_VALUE) { Int.MAX_VALUE + 1 } + // expect(Int.MAX_VALUE) { Int.MIN_VALUE - 1 } } test fun longMinMaxValues() { assertTrue(Long.MIN_VALUE < 0) assertTrue(Long.MAX_VALUE > 0) // overflow behavior - assertEquals(Long.MIN_VALUE, Long.MAX_VALUE + 1) - assertEquals(Long.MAX_VALUE, Long.MIN_VALUE - 1) + expect(Long.MIN_VALUE) { Long.MAX_VALUE + 1 } + expect(Long.MAX_VALUE) { Long.MIN_VALUE - 1 } } test fun shortMinMaxValues() { assertTrue(Short.MIN_VALUE < 0) assertTrue(Short.MAX_VALUE > 0) // overflow behavior - assertEquals(Short.MIN_VALUE, (Short.MAX_VALUE + 1).toShort()) - assertEquals(Short.MAX_VALUE, (Short.MIN_VALUE - 1).toShort()) + expect(Short.MIN_VALUE) { (Short.MAX_VALUE + 1).toShort() } + expect(Short.MAX_VALUE) { (Short.MIN_VALUE - 1).toShort() } } test fun byteMinMaxValues() { assertTrue(Byte.MIN_VALUE < 0) assertTrue(Byte.MAX_VALUE > 0) // overflow behavior - assertEquals(Byte.MIN_VALUE, (Byte.MAX_VALUE + 1).toByte()) - assertEquals(Byte.MAX_VALUE, (Byte.MIN_VALUE - 1).toByte()) + expect(Byte.MIN_VALUE) { (Byte.MAX_VALUE + 1).toByte() } + expect(Byte.MAX_VALUE) { (Byte.MIN_VALUE - 1).toByte() } } + test fun doubleMinMaxValues() { + assertTrue(Double.MIN_VALUE > 0) + assertTrue(Double.MAX_VALUE > 0) + // overflow behavior + expect(Double.POSITIVE_INFINITY) { Double.MAX_VALUE * 2 } + expect(Double.NEGATIVE_INFINITY) {-Double.MAX_VALUE * 2 } + expect(0.0) { Double.MIN_VALUE / 2 } + } + + test fun floatMinMaxValues() { + assertTrue(Float.MIN_VALUE > 0) + assertTrue(Float.MAX_VALUE > 0) + // overflow behavior + expect(Float.POSITIVE_INFINITY) { Float.MAX_VALUE * 2 } + expect(Float.NEGATIVE_INFINITY) { -Float.MAX_VALUE * 2 } + expect(0.0F) { Float.MIN_VALUE / 2.0F } + } + test fun doubleProperties() { - for (value in listOf(1.0, 0.0)) + for (value in listOf(1.0, 0.0, Double.MIN_VALUE, Double.MAX_VALUE)) doTestNumber(value) for (value in listOf(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY)) doTestNumber(value, isInfinite = true) @@ -48,7 +67,7 @@ class NumbersTest { } test fun floatProperties() { - for (value in listOf(1.0F, 0.0F)) + for (value in listOf(1.0F, 0.0F, Float.MAX_VALUE, Float.MIN_VALUE)) doTestNumber(value) for (value in listOf(Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY)) doTestNumber(value, isInfinite = true)