Make Double and Float known values constant

#KT-13887
This commit is contained in:
Ilya Gorbunov
2020-01-21 17:02:49 +03:00
parent 435a7e7e26
commit 1c93911279
7 changed files with 79 additions and 65 deletions
@@ -13,10 +13,10 @@ fun main() {
val finfn = Float.NEGATIVE_INFINITY val finfn = Float.NEGATIVE_INFINITY
} }
// 5 GETSTATIC kotlin/jvm/internal/DoubleCompanionObject.INSTANCE // 0 GETSTATIC kotlin/jvm/internal/DoubleCompanionObject.INSTANCE
// 5 GETSTATIC kotlin/jvm/internal/FloatCompanionObject.INSTANCE // 0 GETSTATIC kotlin/jvm/internal/FloatCompanionObject.INSTANCE
// 2 getMAX_VALUE // 0 getMAX_VALUE
// 2 getMIN_VALUE // 0 getMIN_VALUE
// 2 getNaN // 0 getNaN
// 2 getPOSITIVE_INFINITY // 0 getPOSITIVE_INFINITY
// 2 getNEGATIVE_INFINITY // 0 getNEGATIVE_INFINITY
+10 -10
View File
@@ -898,27 +898,27 @@ public class Float private constructor() : Number(), Comparable<Float> {
/** /**
* A constant holding the smallest *positive* nonzero value of Float. * A constant holding the smallest *positive* nonzero value of Float.
*/ */
public val MIN_VALUE: Float public const val MIN_VALUE: Float = 1.4E-45F
/** /**
* A constant holding the largest positive finite value of Float. * A constant holding the largest positive finite value of Float.
*/ */
public val MAX_VALUE: Float public const val MAX_VALUE: Float = 3.4028235E38F
/** /**
* A constant holding the positive infinity value of Float. * A constant holding the positive infinity value of Float.
*/ */
public val POSITIVE_INFINITY: Float public const val POSITIVE_INFINITY: Float = 1.0F/0.0F
/** /**
* A constant holding the negative infinity value of Float. * A constant holding the negative infinity value of Float.
*/ */
public val NEGATIVE_INFINITY: Float public const val NEGATIVE_INFINITY: Float = -1.0F/0.0F
/** /**
* A constant holding the "not a number" value of Float. * A constant holding the "not a number" value of Float.
*/ */
public val NaN: Float public const val NaN: Float = -(0.0F/0.0F)
} }
/** /**
@@ -1099,27 +1099,27 @@ public class Double private constructor() : Number(), Comparable<Double> {
/** /**
* A constant holding the smallest *positive* nonzero value of Double. * A constant holding the smallest *positive* nonzero value of Double.
*/ */
public val MIN_VALUE: Double public const val MIN_VALUE: Double = 4.9E-324
/** /**
* A constant holding the largest positive finite value of Double. * A constant holding the largest positive finite value of Double.
*/ */
public val MAX_VALUE: Double public const val MAX_VALUE: Double = 1.7976931348623157E308
/** /**
* A constant holding the positive infinity value of Double. * A constant holding the positive infinity value of Double.
*/ */
public val POSITIVE_INFINITY: Double public const val POSITIVE_INFINITY: Double = 1.0/0.0
/** /**
* A constant holding the negative infinity value of Double. * A constant holding the negative infinity value of Double.
*/ */
public val NEGATIVE_INFINITY: Double public const val NEGATIVE_INFINITY: Double = -1.0/0.0
/** /**
* A constant holding the "not a number" value of Double. * A constant holding the "not a number" value of Double.
*/ */
public val NaN: Double public const val NaN: Double = -(0.0/0.0)
} }
/** /**
+8 -8
View File
@@ -47,8 +47,8 @@ class GeneratePrimitives(out: PrintWriter) : BuiltInsSourceGenerator(out) {
PrimitiveType.BYTE -> listOf(java.lang.Byte.MIN_VALUE, java.lang.Byte.MAX_VALUE) PrimitiveType.BYTE -> listOf(java.lang.Byte.MIN_VALUE, java.lang.Byte.MAX_VALUE)
PrimitiveType.SHORT -> listOf(java.lang.Short.MIN_VALUE, java.lang.Short.MAX_VALUE) PrimitiveType.SHORT -> listOf(java.lang.Short.MIN_VALUE, java.lang.Short.MAX_VALUE)
PrimitiveType.LONG -> listOf((java.lang.Long.MIN_VALUE + 1).toString() + "L - 1L", java.lang.Long.MAX_VALUE.toString() + "L") PrimitiveType.LONG -> listOf((java.lang.Long.MIN_VALUE + 1).toString() + "L - 1L", java.lang.Long.MAX_VALUE.toString() + "L")
// PrimitiveType.DOUBLE -> listOf(java.lang.Double.MIN_VALUE, java.lang.Double.MAX_VALUE, "1.0/0.0", "-1.0/0.0", "0.0/0.0") PrimitiveType.DOUBLE -> listOf(java.lang.Double.MIN_VALUE, java.lang.Double.MAX_VALUE, "1.0/0.0", "-1.0/0.0", "-(0.0/0.0)")
// PrimitiveType.FLOAT -> listOf(java.lang.Float.MIN_VALUE, java.lang.Float.MAX_VALUE, "1.0F/0.0F", "-1.0F/0.0F", "0.0F/0.0F").map { it as? String ?: "${it}F" } PrimitiveType.FLOAT -> listOf(java.lang.Float.MIN_VALUE, java.lang.Float.MAX_VALUE, "1.0F/0.0F", "-1.0F/0.0F", "-(0.0F/0.0F)").map { it as? String ?: "${it}F" }
else -> throw IllegalArgumentException("type: $type") else -> throw IllegalArgumentException("type: $type")
} }
@@ -60,32 +60,32 @@ class GeneratePrimitives(out: PrintWriter) : BuiltInsSourceGenerator(out) {
out.print(" companion object {") out.print(" companion object {")
if (kind == PrimitiveType.FLOAT || kind == PrimitiveType.DOUBLE) { if (kind == PrimitiveType.FLOAT || kind == PrimitiveType.DOUBLE) {
//val (minValue, maxValue, posInf, negInf, nan) = primitiveConstants(kind) val (minValue, maxValue, posInf, negInf, nan) = primitiveConstants(kind)
out.println(""" out.println("""
/** /**
* A constant holding the smallest *positive* nonzero value of $className. * A constant holding the smallest *positive* nonzero value of $className.
*/ */
public val MIN_VALUE: $className public const val MIN_VALUE: $className = $minValue
/** /**
* A constant holding the largest positive finite value of $className. * A constant holding the largest positive finite value of $className.
*/ */
public val MAX_VALUE: $className public const val MAX_VALUE: $className = $maxValue
/** /**
* A constant holding the positive infinity value of $className. * A constant holding the positive infinity value of $className.
*/ */
public val POSITIVE_INFINITY: $className public const val POSITIVE_INFINITY: $className = $posInf
/** /**
* A constant holding the negative infinity value of $className. * A constant holding the negative infinity value of $className.
*/ */
public val NEGATIVE_INFINITY: $className public const val NEGATIVE_INFINITY: $className = $negInf
/** /**
* A constant holding the "not a number" value of $className. * A constant holding the "not a number" value of $className.
*/ */
public val NaN: $className""") public const val NaN: $className = $nan""")
} }
if (kind == PrimitiveType.INT || kind == PrimitiveType.LONG || kind == PrimitiveType.SHORT || kind == PrimitiveType.BYTE) { if (kind == PrimitiveType.INT || kind == PrimitiveType.LONG || kind == PrimitiveType.SHORT || kind == PrimitiveType.BYTE) {
val (minValue, maxValue) = primitiveConstants(kind) val (minValue, maxValue) = primitiveConstants(kind)
+10 -20
View File
@@ -680,32 +680,27 @@ public class Float private constructor() : Number(), Comparable<Float> {
/** /**
* A constant holding the smallest *positive* nonzero value of Float. * A constant holding the smallest *positive* nonzero value of Float.
*/ */
@Suppress("MUST_BE_INITIALIZED_OR_BE_ABSTRACT") public const val MIN_VALUE: Float = 1.4E-45F
public val MIN_VALUE: Float
/** /**
* A constant holding the largest positive finite value of Float. * A constant holding the largest positive finite value of Float.
*/ */
@Suppress("MUST_BE_INITIALIZED_OR_BE_ABSTRACT") public const val MAX_VALUE: Float = 3.4028235E38F
public val MAX_VALUE: Float
/** /**
* A constant holding the positive infinity value of Float. * A constant holding the positive infinity value of Float.
*/ */
@Suppress("MUST_BE_INITIALIZED_OR_BE_ABSTRACT") public const val POSITIVE_INFINITY: Float = 1.0F/0.0F
public val POSITIVE_INFINITY: Float
/** /**
* A constant holding the negative infinity value of Float. * A constant holding the negative infinity value of Float.
*/ */
@Suppress("MUST_BE_INITIALIZED_OR_BE_ABSTRACT") public const val NEGATIVE_INFINITY: Float = -1.0F/0.0F
public val NEGATIVE_INFINITY: Float
/** /**
* A constant holding the "not a number" value of Float. * A constant holding the "not a number" value of Float.
*/ */
@Suppress("MUST_BE_INITIALIZED_OR_BE_ABSTRACT") public const val NaN: Float = -(0.0F/0.0F)
public val NaN: Float
} }
/** /**
@@ -890,32 +885,27 @@ public class Double private constructor() : Number(), Comparable<Double> {
/** /**
* A constant holding the smallest *positive* nonzero value of Double. * A constant holding the smallest *positive* nonzero value of Double.
*/ */
@Suppress("MUST_BE_INITIALIZED_OR_BE_ABSTRACT") public const val MIN_VALUE: Double = 4.9E-324
public val MIN_VALUE: Double
/** /**
* A constant holding the largest positive finite value of Double. * A constant holding the largest positive finite value of Double.
*/ */
@Suppress("MUST_BE_INITIALIZED_OR_BE_ABSTRACT") public const val MAX_VALUE: Double = 1.7976931348623157E308
public val MAX_VALUE: Double
/** /**
* A constant holding the positive infinity value of Double. * A constant holding the positive infinity value of Double.
*/ */
@Suppress("MUST_BE_INITIALIZED_OR_BE_ABSTRACT") public const val POSITIVE_INFINITY: Double = 1.0/0.0
public val POSITIVE_INFINITY: Double
/** /**
* A constant holding the negative infinity value of Double. * A constant holding the negative infinity value of Double.
*/ */
@Suppress("MUST_BE_INITIALIZED_OR_BE_ABSTRACT") public const val NEGATIVE_INFINITY: Double = -1.0/0.0
public val NEGATIVE_INFINITY: Double
/** /**
* A constant holding the "not a number" value of Double. * A constant holding the "not a number" value of Double.
*/ */
@Suppress("MUST_BE_INITIALIZED_OR_BE_ABSTRACT") public const val NaN: Double = -(0.0/0.0)
public val NaN: Double
} }
/** /**
@@ -8,37 +8,37 @@ package kotlin.js.internal
@JsName("DoubleCompanionObject") @JsName("DoubleCompanionObject")
internal object DoubleCompanionObject { internal object DoubleCompanionObject {
@JsName("MIN_VALUE") @JsName("MIN_VALUE")
val MIN_VALUE: Double = js("Number.MIN_VALUE") const val MIN_VALUE: Double = 4.9E-324
@JsName("MAX_VALUE") @JsName("MAX_VALUE")
val MAX_VALUE: Double = js("Number.MAX_VALUE") const val MAX_VALUE: Double = 1.7976931348623157E308
@JsName("POSITIVE_INFINITY") @JsName("POSITIVE_INFINITY")
val POSITIVE_INFINITY: Double = js("Number.POSITIVE_INFINITY") const val POSITIVE_INFINITY: Double = 1.0/0.0
@JsName("NEGATIVE_INFINITY") @JsName("NEGATIVE_INFINITY")
val NEGATIVE_INFINITY: Double = js("Number.NEGATIVE_INFINITY") const val NEGATIVE_INFINITY: Double = -1.0/0.0
@JsName("NaN") @JsName("NaN")
val NaN: Double = js("Number.NaN") const val NaN: Double = -(0.0/0.0)
} }
@JsName("FloatCompanionObject") @JsName("FloatCompanionObject")
internal object FloatCompanionObject { internal object FloatCompanionObject {
@JsName("MIN_VALUE") @JsName("MIN_VALUE")
val MIN_VALUE: Float = js("Number.MIN_VALUE") const val MIN_VALUE: Float = 1.4E-45F
@JsName("MAX_VALUE") @JsName("MAX_VALUE")
val MAX_VALUE: Float = js("Number.MAX_VALUE") const val MAX_VALUE: Float = 3.4028235E38F
@JsName("POSITIVE_INFINITY") @JsName("POSITIVE_INFINITY")
val POSITIVE_INFINITY: Float = js("Number.POSITIVE_INFINITY") const val POSITIVE_INFINITY: Float = 1.0F/0.0F
@JsName("NEGATIVE_INFINITY") @JsName("NEGATIVE_INFINITY")
val NEGATIVE_INFINITY: Float = js("Number.NEGATIVE_INFINITY") const val NEGATIVE_INFINITY: Float = -1.0F/0.0F
@JsName("NaN") @JsName("NaN")
val NaN: Float = js("Number.NaN") const val NaN: Float = -(0.0F/0.0F)
} }
@JsName("IntCompanionObject") @JsName("IntCompanionObject")
@@ -6,19 +6,33 @@
package kotlin.jvm.internal package kotlin.jvm.internal
internal object DoubleCompanionObject { internal object DoubleCompanionObject {
val MIN_VALUE: Double = java.lang.Double.MIN_VALUE const val MIN_VALUE: Double = java.lang.Double.MIN_VALUE
val MAX_VALUE: Double = java.lang.Double.MAX_VALUE const val MAX_VALUE: Double = java.lang.Double.MAX_VALUE
val POSITIVE_INFINITY: Double = java.lang.Double.POSITIVE_INFINITY const val POSITIVE_INFINITY: Double = java.lang.Double.POSITIVE_INFINITY
val NEGATIVE_INFINITY: Double = java.lang.Double.NEGATIVE_INFINITY const val NEGATIVE_INFINITY: Double = java.lang.Double.NEGATIVE_INFINITY
val NaN: Double = java.lang.Double.NaN const val NaN: Double = java.lang.Double.NaN
// for binary compatibility with pre 1.4
fun getMIN_VALUE(): Double = java.lang.Double.MIN_VALUE
fun getMAX_VALUE(): Double = java.lang.Double.MAX_VALUE
fun getPOSITIVE_INFINITY(): Double = java.lang.Double.POSITIVE_INFINITY
fun getNEGATIVE_INFINITY(): Double = java.lang.Double.NEGATIVE_INFINITY
fun getNaN(): Double = java.lang.Double.NaN
} }
internal object FloatCompanionObject { internal object FloatCompanionObject {
val MIN_VALUE: Float = java.lang.Float.MIN_VALUE const val MIN_VALUE: Float = java.lang.Float.MIN_VALUE
val MAX_VALUE: Float = java.lang.Float.MAX_VALUE const val MAX_VALUE: Float = java.lang.Float.MAX_VALUE
val POSITIVE_INFINITY: Float = java.lang.Float.POSITIVE_INFINITY const val POSITIVE_INFINITY: Float = java.lang.Float.POSITIVE_INFINITY
val NEGATIVE_INFINITY: Float = java.lang.Float.NEGATIVE_INFINITY const val NEGATIVE_INFINITY: Float = java.lang.Float.NEGATIVE_INFINITY
val NaN: Float = java.lang.Float.NaN const val NaN: Float = java.lang.Float.NaN
// for binary compatibility with pre 1.4
fun getMIN_VALUE(): Float = java.lang.Float.MIN_VALUE
fun getMAX_VALUE(): Float = java.lang.Float.MAX_VALUE
fun getPOSITIVE_INFINITY(): Float = java.lang.Float.POSITIVE_INFINITY
fun getNEGATIVE_INFINITY(): Float = java.lang.Float.NEGATIVE_INFINITY
fun getNaN(): Float = java.lang.Float.NaN
} }
internal object IntCompanionObject { internal object IntCompanionObject {
@@ -3313,6 +3313,11 @@ public final class kotlin/jvm/internal/CollectionToArray {
public final class kotlin/jvm/internal/DoubleCompanionObject { public final class kotlin/jvm/internal/DoubleCompanionObject {
public static final field INSTANCE Lkotlin/jvm/internal/DoubleCompanionObject; public static final field INSTANCE Lkotlin/jvm/internal/DoubleCompanionObject;
public static final field MAX_VALUE D
public static final field MIN_VALUE D
public static final field NEGATIVE_INFINITY D
public static final field NaN D
public static final field POSITIVE_INFINITY D
public final fun getMAX_VALUE ()D public final fun getMAX_VALUE ()D
public final fun getMIN_VALUE ()D public final fun getMIN_VALUE ()D
public final fun getNEGATIVE_INFINITY ()D public final fun getNEGATIVE_INFINITY ()D
@@ -3333,6 +3338,11 @@ public final class kotlin/jvm/internal/EnumCompanionObject {
public final class kotlin/jvm/internal/FloatCompanionObject { public final class kotlin/jvm/internal/FloatCompanionObject {
public static final field INSTANCE Lkotlin/jvm/internal/FloatCompanionObject; public static final field INSTANCE Lkotlin/jvm/internal/FloatCompanionObject;
public static final field MAX_VALUE F
public static final field MIN_VALUE F
public static final field NEGATIVE_INFINITY F
public static final field NaN F
public static final field POSITIVE_INFINITY F
public final fun getMAX_VALUE ()F public final fun getMAX_VALUE ()F
public final fun getMIN_VALUE ()F public final fun getMIN_VALUE ()F
public final fun getNEGATIVE_INFINITY ()F public final fun getNEGATIVE_INFINITY ()F