diff --git a/js/js.libraries/src/core/math.kt b/js/js.libraries/src/core/math.kt index 0c71f2cab4b..38f0a7a5072 100644 --- a/js/js.libraries/src/core/math.kt +++ b/js/js.libraries/src/core/math.kt @@ -23,8 +23,10 @@ import kotlin.js.Math as nativeMath // constants, can't use them from nativeMath as they are not constants there /** Ratio of the circumference of a circle to its diameter, approximately 3.14159. */ +@SinceKotlin("1.2") public const val PI: Double = 3.141592653589793 /** Base of the natural logarithms, approximately 2.71828. */ +@SinceKotlin("1.2") public const val E: Double = 2.718281828459045 // ================ Double Math ======================================== @@ -35,6 +37,7 @@ public const val E: Double = 2.718281828459045 * * - `sin(NaN|+Inf|-Inf)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun sin(a: Double): Double = nativeMath.sin(a) @@ -44,6 +47,7 @@ public inline fun sin(a: Double): Double = nativeMath.sin(a) * * - `cos(NaN|+Inf|-Inf)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun cos(a: Double): Double = nativeMath.cos(a) @@ -53,6 +57,7 @@ public inline fun cos(a: Double): Double = nativeMath.cos(a) * * - `tan(NaN|+Inf|-Inf)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun tan(a: Double): Double = nativeMath.tan(a) @@ -63,6 +68,7 @@ public inline fun tan(a: Double): Double = nativeMath.tan(a) * Special cases: * - `asin(v)` is `NaN`, when `abs(v) > 1` or v is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun asin(a: Double): Double = nativeMath.asin(a) @@ -73,6 +79,7 @@ public inline fun asin(a: Double): Double = nativeMath.asin(a) * Special cases: * - `acos(v)` is `NaN`, when `abs(v) > 1` or v is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun acos(a: Double): Double = nativeMath.acos(a) @@ -83,6 +90,7 @@ public inline fun acos(a: Double): Double = nativeMath.acos(a) * Special cases: * - `atan(NaN)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun atan(a: Double): Double = nativeMath.atan(a) @@ -102,6 +110,7 @@ public inline fun atan(a: Double): Double = nativeMath.atan(a) * - `atan2(-Inf, x)` is `-PI/2` for finite `x` * - `atan2(NaN, x)` and `atan2(y, NaN)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun atan2(y: Double, x: Double): Double = nativeMath.atan2(y, x) @@ -114,6 +123,7 @@ public inline fun atan2(y: Double, x: Double): Double = nativeMath.atan2(y, x) * - `sinh(+Inf)` is `+Inf` * - `sinh(-Inf)` is `-Inf` */ +@SinceKotlin("1.2") @InlineOnly public inline fun sinh(a: Double): Double = nativeMath.sinh(a) @@ -125,6 +135,7 @@ public inline fun sinh(a: Double): Double = nativeMath.sinh(a) * - `cosh(NaN)` is `NaN` * - `cosh(+Inf|-Inf)` is `+Inf` */ +@SinceKotlin("1.2") @InlineOnly public inline fun cosh(a: Double): Double = nativeMath.cosh(a) @@ -137,6 +148,7 @@ public inline fun cosh(a: Double): Double = nativeMath.cosh(a) * - `tanh(+Inf)` is `1.0` * - `tanh(-Inf)` is `-1.0` */ +@SinceKotlin("1.2") @InlineOnly public inline fun tanh(a: Double): Double = nativeMath.tanh(a) @@ -147,6 +159,7 @@ public inline fun tanh(a: Double): Double = nativeMath.tanh(a) * - returns `+Inf` if any of arguments is infinite * - returns `NaN` if any of arguments is `NaN` and the other is not infinite */ +@SinceKotlin("1.2") @InlineOnly public inline fun hypot(x: Double, y: Double): Double = nativeMath.hypot(x, y) @@ -156,6 +169,7 @@ public inline fun hypot(x: Double, y: Double): Double = nativeMath.hypot(x, y) * Special cases: * - `sqrt(x)` is `NaN` when `x < 0` or `x` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun sqrt(a: Double): Double = nativeMath.sqrt(a) @@ -167,6 +181,7 @@ public inline fun sqrt(a: Double): Double = nativeMath.sqrt(a) * - `exp(+Inf)` is `+Inf` * - `exp(-Inf)` is `0.0` */ +@SinceKotlin("1.2") @InlineOnly public inline fun exp(a: Double): Double = nativeMath.exp(a) @@ -182,6 +197,7 @@ public inline fun exp(a: Double): Double = nativeMath.exp(a) * * @see [exp] function. */ +@SinceKotlin("1.2") @InlineOnly public inline fun expm1(a: Double): Double = nativeMath.expm1(a) @@ -195,6 +211,7 @@ public inline fun expm1(a: Double): Double = nativeMath.expm1(a) * - `log(+Inf, b)` is `+Inf` for `b > 1` and `-Inf` for `b < 1` * - `log(0.0, b)` is `-Inf` for `b > 1` and `+Inf` for `b > 1` */ +@SinceKotlin("1.2") public fun log(a: Double, base: Double): Double { if (base <= 0.0 || base == 1.0) return Double.NaN return nativeMath.log(a) / nativeMath.log(base) @@ -209,6 +226,7 @@ public fun log(a: Double, base: Double): Double { * - `ln(+Inf)` is `+Inf` * - `ln(0.0)` is `-Inf` */ +@SinceKotlin("1.2") @InlineOnly public inline fun ln(a: Double): Double = nativeMath.log(a) @@ -217,6 +235,7 @@ public inline fun ln(a: Double): Double = nativeMath.log(a) * * @see [ln] function for special cases. */ +@SinceKotlin("1.2") @InlineOnly public inline fun log10(a: Double): Double = nativeMath.log10(a) @@ -225,6 +244,7 @@ public inline fun log10(a: Double): Double = nativeMath.log10(a) * * @see [ln] function for special cases. */ +@SinceKotlin("1.2") @InlineOnly public inline fun log2(a: Double): Double = nativeMath.log2(a) @@ -242,6 +262,7 @@ public inline fun log2(a: Double): Double = nativeMath.log2(a) * @see [ln] function. * @see [expm1] function */ +@SinceKotlin("1.2") @InlineOnly public inline fun ln1p(a: Double): Double = nativeMath.log1p(a) @@ -253,6 +274,7 @@ public inline fun ln1p(a: Double): Double = nativeMath.log1p(a) * Special cases: * - `ceil(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. */ +@SinceKotlin("1.2") @InlineOnly public inline fun ceil(a: Double): Double = nativeMath.ceil(a).unsafeCast() // TODO: Remove unsafe cast after removing public js.math @@ -264,6 +286,7 @@ public inline fun ceil(a: Double): Double = nativeMath.ceil(a).unsafeCast() @@ -275,6 +298,7 @@ public inline fun floor(a: Double): Double = nativeMath.floor(a).unsafeCast() @@ -300,6 +325,7 @@ public fun round(a: Double): Double { * * @see absoluteValue extension property for [Double] */ +@SinceKotlin("1.2") @InlineOnly public inline fun abs(a: Double): Double = nativeMath.abs(a) @@ -312,6 +338,7 @@ public inline fun abs(a: Double): Double = nativeMath.abs(a) * Special case: * - `sign(NaN)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun sign(a: Double): Double = nativeMath.sign(a) @@ -321,6 +348,7 @@ public inline fun sign(a: Double): Double = nativeMath.sign(a) * * If either value is `NaN`, then the result is `NaN`. */ +@SinceKotlin("1.2") @InlineOnly public inline fun min(a: Double, b: Double): Double = nativeMath.min(a, b) /** @@ -328,6 +356,7 @@ public inline fun min(a: Double, b: Double): Double = nativeMath.min(a, b) * * If either value is `NaN`, then the result is `NaN`. */ +@SinceKotlin("1.2") @InlineOnly public inline fun max(a: Double, b: Double): Double = nativeMath.max(a, b) @@ -344,6 +373,7 @@ public inline fun max(a: Double, b: Double): Double = nativeMath.max(a, b) * - `x.pow(Inf)` is `NaN` for `abs(x) == 1.0` * - `x.pow(y)` is `NaN` for `x < 0` and `y` is finite and not an integer */ +@SinceKotlin("1.2") @InlineOnly public inline fun Double.pow(other: Double): Double = nativeMath.pow(this, other) @@ -352,6 +382,7 @@ public inline fun Double.pow(other: Double): Double = nativeMath.pow(this, other * * See the other overload of [pow] for details. */ +@SinceKotlin("1.2") @InlineOnly public inline fun Double.pow(other: Int): Double = nativeMath.pow(this, other.toDouble()) @@ -363,6 +394,7 @@ public inline fun Double.pow(other: Int): Double = nativeMath.pow(this, other.to * * @see abs function */ +@SinceKotlin("1.2") @InlineOnly public inline val Double.absoluteValue: Double get() = nativeMath.abs(this) @@ -375,6 +407,7 @@ public inline val Double.absoluteValue: Double get() = nativeMath.abs(this) * Special case: * - `NaN.sign` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline val Double.sign: Double get() = nativeMath.sign(this) @@ -383,6 +416,7 @@ public inline val Double.sign: Double get() = nativeMath.sign(this) * * If [sign] is `NaN` the sign of the result is undefined. */ +@SinceKotlin("1.2") public fun Double.withSign(sign: Double): Double = this.absoluteValue * when(sign) { 0.0 -> sign(1 / sign) @@ -392,6 +426,7 @@ public fun Double.withSign(sign: Double): Double = /** * Returns this value with the sign bit same as of the [sign] value. */ +@SinceKotlin("1.2") @InlineOnly public inline fun Double.withSign(sign: Int): Double = this.withSign(sign.toDouble()) @@ -405,6 +440,7 @@ public inline fun Double.withSign(sign: Int): Double = this.withSign(sign.toDoub * * @throws IllegalArgumentException when this value is `NaN` */ +@SinceKotlin("1.2") public fun Double.roundToInt(): Int = when { isNaN() -> throw IllegalArgumentException("Cannot round NaN value.") this > Int.MAX_VALUE -> Int.MAX_VALUE @@ -422,6 +458,7 @@ public fun Double.roundToInt(): Int = when { * * @throws IllegalArgumentException when this value is `NaN` */ +@SinceKotlin("1.2") public fun Double.roundToLong(): Long = when { isNaN() -> throw IllegalArgumentException("Cannot round NaN value.") this > Long.MAX_VALUE -> Long.MAX_VALUE @@ -440,6 +477,7 @@ public fun Double.roundToLong(): Long = when { * * - `sin(NaN|+Inf|-Inf)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun sin(a: Float): Float = nativeMath.sin(a.toDouble()).toFloat() @@ -449,6 +487,7 @@ public inline fun sin(a: Float): Float = nativeMath.sin(a.toDouble()).toFloat() * * - `cos(NaN|+Inf|-Inf)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun cos(a: Float): Float = nativeMath.cos(a.toDouble()).toFloat() @@ -458,6 +497,7 @@ public inline fun cos(a: Float): Float = nativeMath.cos(a.toDouble()).toFloat() * * - `tan(NaN|+Inf|-Inf)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun tan(a: Float): Float = nativeMath.tan(a.toDouble()).toFloat() @@ -468,6 +508,7 @@ public inline fun tan(a: Float): Float = nativeMath.tan(a.toDouble()).toFloat() * Special cases: * - `asin(v)` is `NaN`, when `abs(v) > 1` or v is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun asin(a: Float): Float = nativeMath.asin(a.toDouble()).toFloat() @@ -478,6 +519,7 @@ public inline fun asin(a: Float): Float = nativeMath.asin(a.toDouble()).toFloat( * Special cases: * - `acos(v)` is `NaN`, when `abs(v) > 1` or v is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun acos(a: Float): Float = nativeMath.acos(a.toDouble()).toFloat() @@ -488,6 +530,7 @@ public inline fun acos(a: Float): Float = nativeMath.acos(a.toDouble()).toFloat( * Special cases: * - `atan(NaN)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun atan(a: Float): Float = nativeMath.atan(a.toDouble()).toFloat() @@ -507,6 +550,7 @@ public inline fun atan(a: Float): Float = nativeMath.atan(a.toDouble()).toFloat( * - `atan2(-Inf, x)` is `-PI/2` for finite `x` * - `atan2(NaN, x)` and `atan2(y, NaN)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun atan2(y: Float, x: Float): Float = nativeMath.atan2(y.toDouble(), x.toDouble()).toFloat() @@ -519,6 +563,7 @@ public inline fun atan2(y: Float, x: Float): Float = nativeMath.atan2(y.toDouble * - `sinh(+Inf)` is `+Inf` * - `sinh(-Inf)` is `-Inf` */ +@SinceKotlin("1.2") @InlineOnly public inline fun sinh(a: Float): Float = nativeMath.sinh(a.toDouble()).toFloat() @@ -530,6 +575,7 @@ public inline fun sinh(a: Float): Float = nativeMath.sinh(a.toDouble()).toFloat( * - `cosh(NaN)` is `NaN` * - `cosh(+Inf|-Inf)` is `+Inf` */ +@SinceKotlin("1.2") @InlineOnly public inline fun cosh(a: Float): Float = nativeMath.cosh(a.toDouble()).toFloat() @@ -542,6 +588,7 @@ public inline fun cosh(a: Float): Float = nativeMath.cosh(a.toDouble()).toFloat( * - `tanh(+Inf)` is `1.0` * - `tanh(-Inf)` is `-1.0` */ +@SinceKotlin("1.2") @InlineOnly public inline fun tanh(a: Float): Float = nativeMath.tanh(a.toDouble()).toFloat() @@ -552,6 +599,7 @@ public inline fun tanh(a: Float): Float = nativeMath.tanh(a.toDouble()).toFloat( * - returns `+Inf` if any of arguments is infinite * - returns `NaN` if any of arguments is `NaN` and the other is not infinite */ +@SinceKotlin("1.2") @InlineOnly public inline fun hypot(x: Float, y: Float): Float = nativeMath.hypot(x.toDouble(), y.toDouble()).toFloat() @@ -561,6 +609,7 @@ public inline fun hypot(x: Float, y: Float): Float = nativeMath.hypot(x.toDouble * Special cases: * - `sqrt(x)` is `NaN` when `x < 0` or `x` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun sqrt(a: Float): Float = nativeMath.sqrt(a.toDouble()).toFloat() @@ -572,6 +621,7 @@ public inline fun sqrt(a: Float): Float = nativeMath.sqrt(a.toDouble()).toFloat( * - `exp(+Inf)` is `+Inf` * - `exp(-Inf)` is `0.0` */ +@SinceKotlin("1.2") @InlineOnly public inline fun exp(a: Float): Float = nativeMath.exp(a.toDouble()).toFloat() @@ -587,6 +637,7 @@ public inline fun exp(a: Float): Float = nativeMath.exp(a.toDouble()).toFloat() * * @see [exp] function. */ +@SinceKotlin("1.2") @InlineOnly public inline fun expm1(a: Float): Float = nativeMath.expm1(a.toDouble()).toFloat() @@ -600,6 +651,7 @@ public inline fun expm1(a: Float): Float = nativeMath.expm1(a.toDouble()).toFloa * - `log(+Inf, b)` is `+Inf` for `b > 1` and `-Inf` for `b < 1` * - `log(0.0, b)` is `-Inf` for `b > 1` and `+Inf` for `b > 1` */ +@SinceKotlin("1.2") @InlineOnly public inline fun log(a: Float, base: Float): Float = log(a.toDouble(), base.toDouble()).toFloat() @@ -612,6 +664,7 @@ public inline fun log(a: Float, base: Float): Float = log(a.toDouble(), base.toD * - `ln(+Inf)` is `+Inf` * - `ln(0.0)` is `-Inf` */ +@SinceKotlin("1.2") @InlineOnly public inline fun ln(a: Float): Float = nativeMath.log(a.toDouble()).toFloat() @@ -620,6 +673,7 @@ public inline fun ln(a: Float): Float = nativeMath.log(a.toDouble()).toFloat() * * @see [ln] function for special cases. */ +@SinceKotlin("1.2") @InlineOnly public inline fun log10(a: Float): Float = nativeMath.log10(a.toDouble()).toFloat() @@ -628,6 +682,7 @@ public inline fun log10(a: Float): Float = nativeMath.log10(a.toDouble()).toFloa * * @see [ln] function for special cases. */ +@SinceKotlin("1.2") @InlineOnly public inline fun log2(a: Float): Float = nativeMath.log2(a.toDouble()).toFloat() @@ -645,6 +700,7 @@ public inline fun log2(a: Float): Float = nativeMath.log2(a.toDouble()).toFloat( * @see [ln] function * @see [expm1] function */ +@SinceKotlin("1.2") @InlineOnly public inline fun ln1p(a: Float): Float = nativeMath.log1p(a.toDouble()).toFloat() @@ -656,6 +712,7 @@ public inline fun ln1p(a: Float): Float = nativeMath.log1p(a.toDouble()).toFloat * Special cases: * - `ceil(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. */ +@SinceKotlin("1.2") @InlineOnly public inline fun ceil(a: Float): Float = nativeMath.ceil(a.toDouble()).toFloat() @@ -667,6 +724,7 @@ public inline fun ceil(a: Float): Float = nativeMath.ceil(a.toDouble()).toFloat( * Special cases: * - `floor(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. */ +@SinceKotlin("1.2") @InlineOnly public inline fun floor(a: Float): Float = nativeMath.floor(a.toDouble()).toFloat() @@ -678,6 +736,7 @@ public inline fun floor(a: Float): Float = nativeMath.floor(a.toDouble()).toFloa * Special cases: * - `truncate(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. */ +@SinceKotlin("1.2") @InlineOnly public inline fun truncate(a: Float): Float = truncate(a.toDouble()).toFloat() @@ -687,6 +746,7 @@ public inline fun truncate(a: Float): Float = truncate(a.toDouble()).toFloat() * Special cases: * - `round(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. */ +@SinceKotlin("1.2") @InlineOnly public inline fun round(a: Float): Float = round(a.toDouble()).toFloat() @@ -699,6 +759,7 @@ public inline fun round(a: Float): Float = round(a.toDouble()).toFloat() * * @see absoluteValue extension property for [Float] */ +@SinceKotlin("1.2") @InlineOnly public inline fun abs(a: Float): Float = nativeMath.abs(a.toDouble()).toFloat() @@ -711,6 +772,7 @@ public inline fun abs(a: Float): Float = nativeMath.abs(a.toDouble()).toFloat() * Special case: * - `sign(NaN)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun sign(a: Float): Float = nativeMath.sign(a.toDouble()).toFloat() @@ -721,6 +783,7 @@ public inline fun sign(a: Float): Float = nativeMath.sign(a.toDouble()).toFloat( * * If either value is `NaN`, then the result is `NaN`. */ +@SinceKotlin("1.2") @InlineOnly public inline fun min(a: Float, b: Float): Float = nativeMath.min(a, b) /** @@ -728,6 +791,7 @@ public inline fun min(a: Float, b: Float): Float = nativeMath.min(a, b) * * If either value is `NaN`, then the result is `NaN`. */ +@SinceKotlin("1.2") @InlineOnly public inline fun max(a: Float, b: Float): Float = nativeMath.max(a, b) @@ -745,6 +809,7 @@ public inline fun max(a: Float, b: Float): Float = nativeMath.max(a, b) * - `x.pow(Inf)` is `NaN` for `abs(x) == 1.0` * - `x.pow(y)` is `NaN` for `x < 0` and `y` is finite and not an integer */ +@SinceKotlin("1.2") @InlineOnly public inline fun Float.pow(other: Float): Float = nativeMath.pow(this.toDouble(), other.toDouble()).toFloat() @@ -753,6 +818,7 @@ public inline fun Float.pow(other: Float): Float = nativeMath.pow(this.toDouble( * * See the other overload of [pow] for details. */ +@SinceKotlin("1.2") @InlineOnly public inline fun Float.pow(other: Int): Float = nativeMath.pow(this.toDouble(), other.toDouble()).toFloat() @@ -764,6 +830,7 @@ public inline fun Float.pow(other: Int): Float = nativeMath.pow(this.toDouble(), * * @see abs function */ +@SinceKotlin("1.2") @InlineOnly public inline val Float.absoluteValue: Float get() = nativeMath.abs(this.toDouble()).toFloat() @@ -776,6 +843,7 @@ public inline val Float.absoluteValue: Float get() = nativeMath.abs(this.toDoubl * Special case: * - `NaN.sign` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline val Float.sign: Float get() = nativeMath.sign(this.toDouble()).toFloat() @@ -784,11 +852,13 @@ public inline val Float.sign: Float get() = nativeMath.sign(this.toDouble()).toF * * If [sign] is `NaN` the sign of the result is undefined. */ +@SinceKotlin("1.2") @InlineOnly public inline fun Float.withSign(sign: Float): Float = this.toDouble().withSign(sign.toDouble()).toFloat() /** * Returns this value with the sign bit same as of the [sign] value. */ +@SinceKotlin("1.2") @InlineOnly public inline fun Float.withSign(sign: Int): Float = this.toDouble().withSign(sign.toDouble()).toFloat() @@ -803,6 +873,7 @@ public inline fun Float.withSign(sign: Int): Float = this.toDouble().withSign(si * * @throws IllegalArgumentException when this value is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun Float.roundToInt(): Int = toDouble().roundToInt() @@ -816,6 +887,7 @@ public inline fun Float.roundToInt(): Int = toDouble().roundToInt() * * @throws IllegalArgumentException when this value is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun Float.roundToLong(): Long = toDouble().roundToLong() @@ -831,17 +903,20 @@ public inline fun Float.roundToLong(): Long = toDouble().roundToLong() * @see absoluteValue extension property for [Int] */ // TODO: remove manual 'or' when KT-19290 is fixed +@SinceKotlin("1.2") public fun abs(a: Int): Int = if (a < 0) (-a or 0) else a /** * Returns the smaller of two values. */ +@SinceKotlin("1.2") @InlineOnly public inline fun min(a: Int, b: Int): Int = minOf(a, b) /** * Returns the greater of two values. */ +@SinceKotlin("1.2") @InlineOnly public inline fun max(a: Int, b: Int): Int = maxOf(a, b) @@ -853,6 +928,7 @@ public inline fun max(a: Int, b: Int): Int = maxOf(a, b) * * @see abs function */ +@SinceKotlin("1.2") @InlineOnly public inline val Int.absoluteValue: Int get() = abs(this) @@ -862,6 +938,7 @@ public inline val Int.absoluteValue: Int get() = abs(this) * - `0` if the value is zero, * - `1` if the value is positive */ +@SinceKotlin("1.2") public val Int.sign: Int get() = when { this < 0 -> -1 this > 0 -> 1 @@ -878,17 +955,20 @@ public val Int.sign: Int get() = when { * * @see absoluteValue extension property for [Long] */ +@SinceKotlin("1.2") public fun abs(a: Long): Long = if (a < 0) -a else a /** * Returns the smaller of two values. */ +@SinceKotlin("1.2") @InlineOnly public inline fun min(a: Long, b: Long): Long = minOf(a, b) /** * Returns the greater of two values. */ +@SinceKotlin("1.2") @InlineOnly public inline fun max(a: Long, b: Long): Long = maxOf(a, b) @@ -900,6 +980,7 @@ public inline fun max(a: Long, b: Long): Long = maxOf(a, b) * * @see abs function */ +@SinceKotlin("1.2") @InlineOnly public inline val Long.absoluteValue: Long get() = abs(this) @@ -909,6 +990,7 @@ public inline val Long.absoluteValue: Long get() = abs(this) * - `0` if the value is zero, * - `1` if the value is positive */ +@SinceKotlin("1.2") public val Long.sign: Int get() = when { this < 0 -> -1 this > 0 -> 1 diff --git a/libraries/stdlib/common/src/kotlin/MathH.kt b/libraries/stdlib/common/src/kotlin/MathH.kt new file mode 100644 index 00000000000..85c3a490c0d --- /dev/null +++ b/libraries/stdlib/common/src/kotlin/MathH.kt @@ -0,0 +1,897 @@ +/* + * Copyright 2010-2017 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.math + + + +// constants, can't use them from nativeMath as they are not constants there + +/** Ratio of the circumference of a circle to its diameter, approximately 3.14159. */ +@SinceKotlin("1.2") +public const val PI: Double = 3.141592653589793 +/** Base of the natural logarithms, approximately 2.71828. */ +@SinceKotlin("1.2") +public const val E: Double = 2.718281828459045 + +// ================ Double Math ======================================== + +/** Computes the sine of the angle [a] given in radians. + * + * Special cases: + * + * - `sin(NaN|+Inf|-Inf)` is `NaN` + */ +@SinceKotlin("1.2") +public expect fun sin(a: Double): Double + +/** Computes the cosine of the angle [a] given in radians. + * + * Special cases: + * + * - `cos(NaN|+Inf|-Inf)` is `NaN` + */ +@SinceKotlin("1.2") +public expect fun cos(a: Double): Double + +/** Computes the tangent of the angle [a] given in radians. + * + * Special cases: + * + * - `tan(NaN|+Inf|-Inf)` is `NaN` + */ +@SinceKotlin("1.2") +public expect fun tan(a: Double): Double + +/** + * Computes the arc sine of the value [a]; + * the returned value is an angle in the range from `-PI/2` to `PI/2` radians. + * + * Special cases: + * - `asin(v)` is `NaN`, when `abs(v) > 1` or v is `NaN` + */ +@SinceKotlin("1.2") +public expect fun asin(a: Double): Double + +/** + * Computes the arc cosine of the value [a]; + * the returned value is an angle in the range from `0.0` to `PI` radians. + * + * Special cases: + * - `acos(v)` is `NaN`, when `abs(v) > 1` or v is `NaN` + */ +@SinceKotlin("1.2") +public expect fun acos(a: Double): Double + +/** + * Computes the arc tangent of the value [a]; + * the returned value is an angle in the range from `-PI/2` to `PI/2` radians. + * + * Special cases: + * - `atan(NaN)` is `NaN` + */ +@SinceKotlin("1.2") +public expect fun atan(a: Double): Double + +/** + * Returns the angle `theta` of the polar coordinates `(r, theta)` that correspond + * to the rectangular coordinates `(x, y)` by computing the arc tangent of the value [y] / [x]; + * the returned value is an angle in the range from `-PI` to `PI` radians. + * + * Special cases: + * - `atan2(0.0, 0.0)` is `0.0` + * - `atan2(0.0, x)` is `0.0` for `x > 0` and `PI` for `x < 0` + * - `atan2(-0.0, x)` is `-0.0` for 'x > 0` and `-PI` for `x < 0` + * - `atan2(y, +Inf)` is `0.0` for `0 < y < +Inf` and `-0.0` for '-Inf < y < 0` + * - `atan2(y, -Inf)` is `PI` for `0 < y < +Inf` and `-PI` for `-Inf < y < 0` + * - `atan2(y, 0.0)` is `PI/2` for `y > 0` and `-PI/2` for `y < 0` + * - `atan2(+Inf, x)` is `PI/2` for finite `x`y + * - `atan2(-Inf, x)` is `-PI/2` for finite `x` + * - `atan2(NaN, x)` and `atan2(y, NaN)` is `NaN` + */ +@SinceKotlin("1.2") +public expect fun atan2(y: Double, x: Double): Double + +/** + * Computes the hyperbolic sine of the value [a]. + * + * Special cases: + * + * - `sinh(NaN)` is `NaN` + * - `sinh(+Inf)` is `+Inf` + * - `sinh(-Inf)` is `-Inf` + */ +@SinceKotlin("1.2") +public expect fun sinh(a: Double): Double + +/** + * Computes the hyperbolic cosine of the value [a]. + * + * Special cases: + * + * - `cosh(NaN)` is `NaN` + * - `cosh(+Inf|-Inf)` is `+Inf` + */ +@SinceKotlin("1.2") +public expect fun cosh(a: Double): Double + +/** + * Computes the hyperbolic tangent of the value [a]. + * + * Special cases: + * + * - `tanh(NaN)` is `NaN` + * - `tanh(+Inf)` is `1.0` + * - `tanh(-Inf)` is `-1.0` + */ +@SinceKotlin("1.2") +public expect fun tanh(a: Double): Double + +/** + * Computes `sqrt(x^2 + y^2)` without intermediate overflow or underflow. + * + * Special cases: + * - returns `+Inf` if any of arguments is infinite + * - returns `NaN` if any of arguments is `NaN` and the other is not infinite + */ +@SinceKotlin("1.2") +public expect fun hypot(x: Double, y: Double): Double + +/** + * Computes the positive square root of the value [a]. + * + * Special cases: + * - `sqrt(x)` is `NaN` when `x < 0` or `x` is `NaN` + */ +@SinceKotlin("1.2") +public expect fun sqrt(a: Double): Double + +/** + * Computes Euler's number `e` raised to the power of the value [a]. + * + * Special cases: + * - `exp(NaN)` is `NaN` + * - `exp(+Inf)` is `+Inf` + * - `exp(-Inf)` is `0.0` + */ +@SinceKotlin("1.2") +public expect fun exp(a: Double): Double + +/** + * Computes `exp(a) - 1`. + * + * This function can be implemented to produce more precise result for [a] near zero. + * + * Special cases: + * - `expm1(NaN)` is `NaN` + * - `expm1(+Inf)` is `+Inf` + * - `expm1(-Inf)` is `-1.0` + * + * @see [exp] function. + */ +@SinceKotlin("1.2") +public expect fun expm1(a: Double): Double + +/** + * Computes the logarithm in the given [base] of the [a] value. + * + * Special cases: + * - `log(a, b)` is `NaN` if either `a` or `b` are `NaN` + * - `log(a, b)` is `NaN` when `a < 0` or `b <= 0` or `b == 1.0` + * - `log(+Inf, +Inf)` is `NaN` + * - `log(+Inf, b)` is `+Inf` for `b > 1` and `-Inf` for `b < 1` + * - `log(0.0, b)` is `-Inf` for `b > 1` and `+Inf` for `b > 1` + */ +@SinceKotlin("1.2") +public expect fun log(a: Double, base: Double): Double + +/** + * Computes the natural logarithm (base `E`) of the [a] value. + * + * Special cases: + * - `ln(NaN)` is `NaN` + * - `ln(x)` is `NaN` when `x < 0.0` + * - `ln(+Inf)` is `+Inf` + * - `ln(0.0)` is `-Inf` + */ +@SinceKotlin("1.2") +public expect fun ln(a: Double): Double + +/** + * Computes the decimal logarithm (base 10) of the [a] value. + * + * @see [ln] function for special cases. + */ +@SinceKotlin("1.2") +public expect fun log10(a: Double): Double + +/** + * Computes the binary logarithm (base 2) of the [a] value. + * + * @see [ln] function for special cases. + */ +@SinceKotlin("1.2") +public expect fun log2(a: Double): Double + +/** + * Computes `log(a + 1)`. + * + * This function can be implemented to produce more precise result for [a] near zero. + * + * Special cases: + * - `log1p(NaN)` is `NaN` + * - `log1p(x)` is `NaN` where `x < -1.0` + * - `log1p(-1.0)` is `-Inf` + * - `log1p(+Inf)` is `+Inf` + * + * @see [ln] function. + * @see [expm1] function + */ +@SinceKotlin("1.2") +public expect fun ln1p(a: Double): Double + +/** + * Rounds the given value [a] to an integer towards positive infinity. + + * @return the smallest double value that is greater than the given value [a] and is a mathematical integer. + * + * Special cases: + * - `ceil(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. + */ +@SinceKotlin("1.2") +public expect fun ceil(a: Double): Double + +/** + * Rounds the given value [a] to an integer towards negative infinity. + + * @return the largest double value that is smaller than the given value [a] and is a mathematical integer. + * + * Special cases: + * - `floor(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. + */ +@SinceKotlin("1.2") +public expect fun floor(a: Double): Double + +/** + * Rounds the given value [a] to an integer towards zero. + * + * @return the value [a] having its fractional part truncated. + * + * Special cases: + * - `truncate(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. + */ +@SinceKotlin("1.2") +public expect fun truncate(a: Double): Double + +/** + * Rounds the given value [a] towards the closest integer with ties rounded towards even integer. + * + * Special cases: + * - `round(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. + */ +@SinceKotlin("1.2") +public expect fun round(a: Double): Double + +/** + * Returns the absolute value of the given value [a]. + * + * Special cases: + * - `abs(NaN)` is `NaN` + * + * @see absoluteValue extension property for [Double] + */ +@SinceKotlin("1.2") +public expect fun abs(a: Double): Double + +/** + * Returns the sign of the given value [a]: + * - `-1.0` if the value is negative, + * - zero if the value is zero, + * - `1.0` if the value is positive + * + * Special case: + * - `sign(NaN)` is `NaN` + */ +@SinceKotlin("1.2") +public expect fun sign(a: Double): Double + + +/** + * Returns the smaller of two values. + * + * If either value is `NaN`, then the result is `NaN`. + */ +@SinceKotlin("1.2") +public expect fun min(a: Double, b: Double): Double +/** + * Returns the greater of two values. + * + * If either value is `NaN`, then the result is `NaN`. + */ +@SinceKotlin("1.2") +public expect fun max(a: Double, b: Double): Double + +// extensions + +/** + * Raises this value to the power [other]. + * + * Special cases: + * - `x.pow(0.0)` is `1.0` + * - `x.pow(1.0) == x` + * - `x.pow(NaN)` is `NaN` + * - `NaN.pow(x)` is `NaN` for `x != 0.0` + * - `x.pow(Inf)` is `NaN` for `abs(x) == 1.0` + * - `x.pow(y)` is `NaN` for `x < 0` and `y` is finite and not an integer + */ +@SinceKotlin("1.2") +public expect fun Double.pow(other: Double): Double + +/** + * Raises this value to the integer power [other]. + * + * See the other overload of [pow] for details. + */ +@SinceKotlin("1.2") +public expect fun Double.pow(other: Int): Double + +/** + * Returns the absolute value of this value. + * + * Special cases: + * - `NaN.absoluteValue` is `NaN` + * + * @see abs function + */ +@SinceKotlin("1.2") +public expect val Double.absoluteValue: Double + +/** + * Returns the sign of this value: + * - `-1.0` if the value is negative, + * - zero if the value is zero, + * - `1.0` if the value is positive + * + * Special case: + * - `NaN.sign` is `NaN` + */ +@SinceKotlin("1.2") +public expect val Double.sign: Double + +/** + * Returns this value with the sign bit same as of the [sign] value. + * + * If [sign] is `NaN` the sign of the result is undefined. + */ +@SinceKotlin("1.2") +public expect fun Double.withSign(sign: Double): Double + +/** + * Returns this value with the sign bit same as of the [sign] value. + */ +@SinceKotlin("1.2") +public expect fun Double.withSign(sign: Int): Double + +/** + * Rounds this [Double] value to the nearest integer and converts the result to [Int]. + * Ties are rounded towards positive infinity. + * + * Special cases: + * - `x.roundToInt() == Int.MAX_VALUE` when `x > Int.MAX_VALUE` + * - `x.roundToInt() == Int.MIN_VALUE` when `x < Int.MIN_VALUE` + * + * @throws IllegalArgumentException when this value is `NaN` + */ +@SinceKotlin("1.2") +public expect fun Double.roundToInt(): Int + +/** + * Rounds this [Double] value to the nearest integer and converts the result to [Long]. + * Ties are rounded towards positive infinity. + * + * Special cases: + * - `x.roundToLong() == Long.MAX_VALUE` when `x > Long.MAX_VALUE` + * - `x.roundToLong() == Long.MIN_VALUE` when `x < Long.MIN_VALUE` + * + * @throws IllegalArgumentException when this value is `NaN` + */ +@SinceKotlin("1.2") +public expect fun Double.roundToLong(): Long + + + + +// ================ Float Math ======================================== + +/** Computes the sine of the angle [a] given in radians. + * + * Special cases: + * + * - `sin(NaN|+Inf|-Inf)` is `NaN` + */ +@SinceKotlin("1.2") +public expect fun sin(a: Float): Float + +/** Computes the cosine of the angle [a] given in radians. + * + * Special cases: + * + * - `cos(NaN|+Inf|-Inf)` is `NaN` + */ +@SinceKotlin("1.2") +public expect fun cos(a: Float): Float + +/** Computes the tangent of the angle [a] given in radians. + * + * Special cases: + * + * - `tan(NaN|+Inf|-Inf)` is `NaN` + */ +@SinceKotlin("1.2") +public expect fun tan(a: Float): Float + +/** + * Computes the arc sine of the value [a]; + * the returned value is an angle in the range from `-PI/2` to `PI/2` radians. + * + * Special cases: + * - `asin(v)` is `NaN`, when `abs(v) > 1` or v is `NaN` + */ +@SinceKotlin("1.2") +public expect fun asin(a: Float): Float + +/** + * Computes the arc cosine of the value [a]; + * the returned value is an angle in the range from `0.0` to `PI` radians. + * + * Special cases: + * - `acos(v)` is `NaN`, when `abs(v) > 1` or v is `NaN` + */ +@SinceKotlin("1.2") +public expect fun acos(a: Float): Float + +/** + * Computes the arc tangent of the value [a]; + * the returned value is an angle in the range from `-PI/2` to `PI/2` radians. + * + * Special cases: + * - `atan(NaN)` is `NaN` + */ +@SinceKotlin("1.2") +public expect fun atan(a: Float): Float + +/** + * Returns the angle `theta` of the polar coordinates `(r, theta)` that correspond + * to the rectangular coordinates `(x, y)` by computing the arc tangent of the value [y] / [x]; + * the returned value is an angle in the range from `-PI` to `PI` radians. + * + * Special cases: + * - `atan2(0.0, 0.0)` is `0.0` + * - `atan2(0.0, x)` is `0.0` for `x > 0` and `PI` for `x < 0` + * - `atan2(-0.0, x)` is `-0.0` for 'x > 0` and `-PI` for `x < 0` + * - `atan2(y, +Inf)` is `0.0` for `0 < y < +Inf` and `-0.0` for '-Inf < y < 0` + * - `atan2(y, -Inf)` is `PI` for `0 < y < +Inf` and `-PI` for `-Inf < y < 0` + * - `atan2(y, 0.0)` is `PI/2` for `y > 0` and `-PI/2` for `y < 0` + * - `atan2(+Inf, x)` is `PI/2` for finite `x`y + * - `atan2(-Inf, x)` is `-PI/2` for finite `x` + * - `atan2(NaN, x)` and `atan2(y, NaN)` is `NaN` + */ +@SinceKotlin("1.2") +public expect fun atan2(y: Float, x: Float): Float + +/** + * Computes the hyperbolic sine of the value [a]. + * + * Special cases: + * + * - `sinh(NaN)` is `NaN` + * - `sinh(+Inf)` is `+Inf` + * - `sinh(-Inf)` is `-Inf` + */ +@SinceKotlin("1.2") +public expect fun sinh(a: Float): Float + +/** + * Computes the hyperbolic cosine of the value [a]. + * + * Special cases: + * + * - `cosh(NaN)` is `NaN` + * - `cosh(+Inf|-Inf)` is `+Inf` + */ +@SinceKotlin("1.2") +public expect fun cosh(a: Float): Float + +/** + * Computes the hyperbolic tangent of the value [a]. + * + * Special cases: + * + * - `tanh(NaN)` is `NaN` + * - `tanh(+Inf)` is `1.0` + * - `tanh(-Inf)` is `-1.0` + */ +@SinceKotlin("1.2") +public expect fun tanh(a: Float): Float + +/** + * Computes `sqrt(x^2 + y^2)` without intermediate overflow or underflow. + * + * Special cases: + * - returns `+Inf` if any of arguments is infinite + * - returns `NaN` if any of arguments is `NaN` and the other is not infinite + */ +@SinceKotlin("1.2") +public expect fun hypot(x: Float, y: Float): Float + +/** + * Computes the positive square root of the value [a]. + * + * Special cases: + * - `sqrt(x)` is `NaN` when `x < 0` or `x` is `NaN` + */ +@SinceKotlin("1.2") +public expect fun sqrt(a: Float): Float + +/** + * Computes Euler's number `e` raised to the power of the value [a]. + * + * Special cases: + * - `exp(NaN)` is `NaN` + * - `exp(+Inf)` is `+Inf` + * - `exp(-Inf)` is `0.0` + */ +@SinceKotlin("1.2") +public expect fun exp(a: Float): Float + +/** + * Computes `exp(a) - 1`. + * + * This function can be implemented to produce more precise result for [a] near zero. + * + * Special cases: + * - `expm1(NaN)` is `NaN` + * - `expm1(+Inf)` is `+Inf` + * - `expm1(-Inf)` is `-1.0` + * + * @see [exp] function. + */ +@SinceKotlin("1.2") +public expect fun expm1(a: Float): Float + +/** + * Computes the logarithm in the given [base] of the [a] value. + * + * Special cases: + * - `log(a, b)` is `NaN` if either `a` or `b` are `NaN` + * - `log(a, b)` is `NaN` when `a < 0` or `b <= 0` or `b == 1.0` + * - `log(+Inf, +Inf)` is `NaN` + * - `log(+Inf, b)` is `+Inf` for `b > 1` and `-Inf` for `b < 1` + * - `log(0.0, b)` is `-Inf` for `b > 1` and `+Inf` for `b > 1` + */ +@SinceKotlin("1.2") +public expect fun log(a: Float, base: Float): Float + +/** + * Computes the natural logarithm (base `E`) of the [a] value. + * + * Special cases: + * - `ln(NaN)` is `NaN` + * - `ln(x)` is `NaN` when `x < 0.0` + * - `ln(+Inf)` is `+Inf` + * - `ln(0.0)` is `-Inf` + */ +@SinceKotlin("1.2") +public expect fun ln(a: Float): Float + +/** + * Computes the decimal logarithm (base 10) of the [a] value. + * + * @see [ln] function for special cases. + */ +@SinceKotlin("1.2") +public expect fun log10(a: Float): Float + +/** + * Computes the binary logarithm (base 2) of the [a] value. + * + * @see [ln] function for special cases. + */ +@SinceKotlin("1.2") +public expect fun log2(a: Float): Float + +/** + * Computes `log(a + 1)`. + * + * This function can be implemented to produce more precise result for [a] near zero. + * + * Special cases: + * - `log1p(NaN)` is `NaN` + * - `log1p(x)` is `NaN` where `x < -1.0` + * - `log1p(-1.0)` is `-Inf` + * - `log1p(+Inf)` is `+Inf` + * + * @see [ln] function + * @see [expm1] function + */ +@SinceKotlin("1.2") +public expect fun ln1p(a: Float): Float + +/** + * Rounds the given value [a] to an integer towards positive infinity. + + * @return the smallest Float value that is greater than the given value [a] and is a mathematical integer. + * + * Special cases: + * - `ceil(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. + */ +@SinceKotlin("1.2") +public expect fun ceil(a: Float): Float + +/** + * Rounds the given value [a] to an integer towards negative infinity. + + * @return the largest Float value that is smaller than the given value [a] and is a mathematical integer. + * + * Special cases: + * - `floor(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. + */ +@SinceKotlin("1.2") +public expect fun floor(a: Float): Float + +/** + * Rounds the given value [a] to an integer towards zero. + * + * @return the value [a] having its fractional part truncated. + * + * Special cases: + * - `truncate(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. + */ +@SinceKotlin("1.2") +public expect fun truncate(a: Float): Float + +/** + * Rounds the given value [a] towards the closest integer with ties rounded towards even integer. + * + * Special cases: + * - `round(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. + */ +@SinceKotlin("1.2") +public expect fun round(a: Float): Float + + +/** + * Returns the absolute value of the given value [a]. + * + * Special cases: + * - `abs(NaN)` is `NaN` + * + * @see absoluteValue extension property for [Float] + */ +@SinceKotlin("1.2") +public expect fun abs(a: Float): Float + +/** + * Returns the sign of the given value [a]: + * - `-1.0` if the value is negative, + * - zero if the value is zero, + * - `1.0` if the value is positive + * + * Special case: + * - `sign(NaN)` is `NaN` + */ +@SinceKotlin("1.2") +public expect fun sign(a: Float): Float + + + +/** + * Returns the smaller of two values. + * + * If either value is `NaN`, then the result is `NaN`. + */ +@SinceKotlin("1.2") +public expect fun min(a: Float, b: Float): Float +/** + * Returns the greater of two values. + * + * If either value is `NaN`, then the result is `NaN`. + */ +@SinceKotlin("1.2") +public expect fun max(a: Float, b: Float): Float + +// extensions + + +/** + * Raises this value to the power [other]. + * + * Special cases: + * - `x.pow(0.0)` is `1.0` + * - `x.pow(1.0) == x` + * - `x.pow(NaN)` is `NaN` + * - `NaN.pow(x)` is `NaN` for `x != 0.0` + * - `x.pow(Inf)` is `NaN` for `abs(x) == 1.0` + * - `x.pow(y)` is `NaN` for `x < 0` and `y` is finite and not an integer + */ +@SinceKotlin("1.2") +public expect fun Float.pow(other: Float): Float + +/** + * Raises this value to the integer power [other]. + * + * See the other overload of [pow] for details. + */ +@SinceKotlin("1.2") +public expect fun Float.pow(other: Int): Float + +/** + * Returns the absolute value of this value. + * + * Special cases: + * - `NaN.absoluteValue` is `NaN` + * + * @see abs function + */ +@SinceKotlin("1.2") +public expect val Float.absoluteValue: Float + +/** + * Returns the sign of this value: + * - `-1.0` if the value is negative, + * - zero if the value is zero, + * - `1.0` if the value is positive + * + * Special case: + * - `NaN.sign` is `NaN` + */ +@SinceKotlin("1.2") +public expect val Float.sign: Float + +/** + * Returns this value with the sign bit same as of the [sign] value. + * + * If [sign] is `NaN` the sign of the result is undefined. + */ +@SinceKotlin("1.2") +public expect fun Float.withSign(sign: Float): Float +/** + * Returns this value with the sign bit same as of the [sign] value. + */ +@SinceKotlin("1.2") +public expect fun Float.withSign(sign: Int): Float + + +/** + * Rounds this [Float] value to the nearest integer and converts the result to [Int]. + * Ties are rounded towards positive infinity. + * + * Special cases: + * - `x.roundToInt() == Int.MAX_VALUE` when `x > Int.MAX_VALUE` + * - `x.roundToInt() == Int.MIN_VALUE` when `x < Int.MIN_VALUE` + * + * @throws IllegalArgumentException when this value is `NaN` + */ +@SinceKotlin("1.2") +public expect fun Float.roundToInt(): Int + +/** + * Rounds this [Float] value to the nearest integer and converts the result to [Long]. + * Ties are rounded towards positive infinity. + * + * Special cases: + * - `x.roundToLong() == Long.MAX_VALUE` when `x > Long.MAX_VALUE` + * - `x.roundToLong() == Long.MIN_VALUE` when `x < Long.MIN_VALUE` + * + * @throws IllegalArgumentException when this value is `NaN` + */ +@SinceKotlin("1.2") +public expect fun Float.roundToLong(): Long + + + + +/** + * Returns the absolute value of the given value [a]. + * + * Special cases: + * - `abs(Int.MIN_VALUE)` is `Int.MIN_VALUE` due to an overflow + * + * @see absoluteValue extension property for [Int] + */ +@SinceKotlin("1.2") +public expect fun abs(a: Int): Int + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.2") +public expect fun min(a: Int, b: Int): Int + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.2") +public expect fun max(a: Int, b: Int): Int + +/** + * Returns the absolute value of this value. + * + * Special cases: + * - `Int.MIN_VALUE.absoluteValue` is `Int.MIN_VALUE` due to an overflow + * + * @see abs function + */ +@SinceKotlin("1.2") +public expect val Int.absoluteValue: Int + +/** + * Returns the sign of this value: + * - `-1` if the value is negative, + * - `0` if the value is zero, + * - `1` if the value is positive + */ +@SinceKotlin("1.2") +public expect val Int.sign: Int + + + +/** + * Returns the absolute value of the given value [a]. + * + * Special cases: + * - `abs(Long.MIN_VALUE)` is `Long.MIN_VALUE` due to an overflow + * + * @see absoluteValue extension property for [Long] + */ +@SinceKotlin("1.2") +public expect fun abs(a: Long): Long + +/** + * Returns the smaller of two values. + */ +@SinceKotlin("1.2") +public expect fun min(a: Long, b: Long): Long + +/** + * Returns the greater of two values. + */ +@SinceKotlin("1.2") +public expect fun max(a: Long, b: Long): Long + +/** + * Returns the absolute value of this value. + * + * Special cases: + * - `Long.MIN_VALUE.absoluteValue` is `Long.MIN_VALUE` due to an overflow + * + * @see abs function + */ +@SinceKotlin("1.2") +public expect val Long.absoluteValue: Long + +/** + * Returns the sign of this value: + * - `-1` if the value is negative, + * - `0` if the value is zero, + * - `1` if the value is positive + */ +@SinceKotlin("1.2") +public expect val Long.sign: Int + + + + diff --git a/libraries/stdlib/src/kotlin/util/MathJVM.kt b/libraries/stdlib/src/kotlin/util/MathJVM.kt index 3bdc2f2b6be..c14ad30e472 100644 --- a/libraries/stdlib/src/kotlin/util/MathJVM.kt +++ b/libraries/stdlib/src/kotlin/util/MathJVM.kt @@ -24,8 +24,10 @@ import java.lang.Math as nativeMath // constants /** Ratio of the circumference of a circle to its diameter, approximately 3.14159. */ +@SinceKotlin("1.2") public const val PI: Double = nativeMath.PI /** Base of the natural logarithms, approximately 2.71828. */ +@SinceKotlin("1.2") public const val E: Double = nativeMath.E /** Natural logarithm of 2.0, used to compute [log2] function */ private val LN2: Double = ln(2.0) @@ -38,6 +40,7 @@ private val LN2: Double = ln(2.0) * * - `sin(NaN|+Inf|-Inf)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun sin(a: Double): Double = nativeMath.sin(a) @@ -47,6 +50,7 @@ public inline fun sin(a: Double): Double = nativeMath.sin(a) * * - `cos(NaN|+Inf|-Inf)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun cos(a: Double): Double = nativeMath.cos(a) @@ -56,6 +60,7 @@ public inline fun cos(a: Double): Double = nativeMath.cos(a) * * - `tan(NaN|+Inf|-Inf)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun tan(a: Double): Double = nativeMath.tan(a) @@ -66,6 +71,7 @@ public inline fun tan(a: Double): Double = nativeMath.tan(a) * Special cases: * - `asin(v)` is `NaN`, when `abs(v) > 1` or v is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun asin(a: Double): Double = nativeMath.asin(a) @@ -76,6 +82,7 @@ public inline fun asin(a: Double): Double = nativeMath.asin(a) * Special cases: * - `acos(v)` is `NaN`, when `abs(v) > 1` or v is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun acos(a: Double): Double = nativeMath.acos(a) @@ -86,6 +93,7 @@ public inline fun acos(a: Double): Double = nativeMath.acos(a) * Special cases: * - `atan(NaN)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun atan(a: Double): Double = nativeMath.atan(a) @@ -105,6 +113,7 @@ public inline fun atan(a: Double): Double = nativeMath.atan(a) * - `atan2(-Inf, x)` is `-PI/2` for finite `x` * - `atan2(NaN, x)` and `atan2(y, NaN)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun atan2(y: Double, x: Double): Double = nativeMath.atan2(y, x) @@ -117,6 +126,7 @@ public inline fun atan2(y: Double, x: Double): Double = nativeMath.atan2(y, x) * - `sinh(+Inf)` is `+Inf` * - `sinh(-Inf)` is `-Inf` */ +@SinceKotlin("1.2") @InlineOnly public inline fun sinh(a: Double): Double = nativeMath.sinh(a) @@ -128,6 +138,7 @@ public inline fun sinh(a: Double): Double = nativeMath.sinh(a) * - `cosh(NaN)` is `NaN` * - `cosh(+Inf|-Inf)` is `+Inf` */ +@SinceKotlin("1.2") @InlineOnly public inline fun cosh(a: Double): Double = nativeMath.cosh(a) @@ -140,6 +151,7 @@ public inline fun cosh(a: Double): Double = nativeMath.cosh(a) * - `tanh(+Inf)` is `1.0` * - `tanh(-Inf)` is `-1.0` */ +@SinceKotlin("1.2") @InlineOnly public inline fun tanh(a: Double): Double = nativeMath.tanh(a) @@ -150,6 +162,7 @@ public inline fun tanh(a: Double): Double = nativeMath.tanh(a) * - returns `+Inf` if any of arguments is infinite * - returns `NaN` if any of arguments is `NaN` and the other is not infinite */ +@SinceKotlin("1.2") @InlineOnly public inline fun hypot(x: Double, y: Double): Double = nativeMath.hypot(x, y) @@ -159,6 +172,7 @@ public inline fun hypot(x: Double, y: Double): Double = nativeMath.hypot(x, y) * Special cases: * - `sqrt(x)` is `NaN` when `x < 0` or `x` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun sqrt(a: Double): Double = nativeMath.sqrt(a) @@ -170,6 +184,7 @@ public inline fun sqrt(a: Double): Double = nativeMath.sqrt(a) * - `exp(+Inf)` is `+Inf` * - `exp(-Inf)` is `0.0` */ +@SinceKotlin("1.2") @InlineOnly public inline fun exp(a: Double): Double = nativeMath.exp(a) @@ -185,6 +200,7 @@ public inline fun exp(a: Double): Double = nativeMath.exp(a) * * @see [exp] function. */ +@SinceKotlin("1.2") @InlineOnly public inline fun expm1(a: Double): Double = nativeMath.expm1(a) @@ -198,6 +214,7 @@ public inline fun expm1(a: Double): Double = nativeMath.expm1(a) * - `log(+Inf, b)` is `+Inf` for `b > 1` and `-Inf` for `b < 1` * - `log(0.0, b)` is `-Inf` for `b > 1` and `+Inf` for `b > 1` */ +@SinceKotlin("1.2") public fun log(a: Double, base: Double): Double { if (base <= 0.0 || base == 1.0) return Double.NaN return nativeMath.log(a) / nativeMath.log(base) @@ -212,6 +229,7 @@ public fun log(a: Double, base: Double): Double { * - `ln(+Inf)` is `+Inf` * - `ln(0.0)` is `-Inf` */ +@SinceKotlin("1.2") @InlineOnly public inline fun ln(a: Double): Double = nativeMath.log(a) @@ -220,6 +238,7 @@ public inline fun ln(a: Double): Double = nativeMath.log(a) * * @see [ln] function for special cases. */ +@SinceKotlin("1.2") @InlineOnly public inline fun log10(a: Double): Double = nativeMath.log10(a) @@ -228,6 +247,7 @@ public inline fun log10(a: Double): Double = nativeMath.log10(a) * * @see [ln] function for special cases. */ +@SinceKotlin("1.2") public fun log2(a: Double): Double = nativeMath.log(a) / LN2 /** @@ -244,6 +264,7 @@ public fun log2(a: Double): Double = nativeMath.log(a) / LN2 * @see [ln] function * @see [expm1] function */ +@SinceKotlin("1.2") @InlineOnly public inline fun ln1p(a: Double): Double = nativeMath.log1p(a) @@ -255,6 +276,7 @@ public inline fun ln1p(a: Double): Double = nativeMath.log1p(a) * Special cases: * - `ceil(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. */ +@SinceKotlin("1.2") @InlineOnly public inline fun ceil(a: Double): Double = nativeMath.ceil(a) @@ -266,6 +288,7 @@ public inline fun ceil(a: Double): Double = nativeMath.ceil(a) * Special cases: * - `floor(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. */ +@SinceKotlin("1.2") @InlineOnly public inline fun floor(a: Double): Double = nativeMath.floor(a) @@ -277,6 +300,7 @@ public inline fun floor(a: Double): Double = nativeMath.floor(a) * Special cases: * - `truncate(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. */ +@SinceKotlin("1.2") public fun truncate(a: Double): Double = when { a.isNaN() || a.isInfinite() -> a a > 0 -> floor(a) @@ -289,6 +313,7 @@ public fun truncate(a: Double): Double = when { * Special cases: * - `round(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. */ +@SinceKotlin("1.2") @InlineOnly public inline fun round(a: Double): Double = nativeMath.rint(a) @@ -301,6 +326,7 @@ public inline fun round(a: Double): Double = nativeMath.rint(a) * * @see absoluteValue extension property for [Double] */ +@SinceKotlin("1.2") @InlineOnly public inline fun abs(a: Double): Double = nativeMath.abs(a) @@ -313,6 +339,7 @@ public inline fun abs(a: Double): Double = nativeMath.abs(a) * Special case: * - `sign(NaN)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun sign(a: Double): Double = nativeMath.signum(a) @@ -323,6 +350,7 @@ public inline fun sign(a: Double): Double = nativeMath.signum(a) * * If either value is `NaN`, then the result is `NaN`. */ +@SinceKotlin("1.2") @InlineOnly public inline fun min(a: Double, b: Double): Double = nativeMath.min(a, b) /** @@ -330,6 +358,7 @@ public inline fun min(a: Double, b: Double): Double = nativeMath.min(a, b) * * If either value is `NaN`, then the result is `NaN`. */ +@SinceKotlin("1.2") @InlineOnly public inline fun max(a: Double, b: Double): Double = nativeMath.max(a, b) @@ -347,6 +376,7 @@ public inline fun max(a: Double, b: Double): Double = nativeMath.max(a, b) * - `x.pow(Inf)` is `NaN` for `abs(x) == 1.0` * - `x.pow(y)` is `NaN` for `x < 0` and `y` is finite and not an integer */ +@SinceKotlin("1.2") @InlineOnly public inline fun Double.pow(other: Double): Double = nativeMath.pow(this, other) @@ -355,6 +385,7 @@ public inline fun Double.pow(other: Double): Double = nativeMath.pow(this, other * * See the other overload of [pow] for details. */ +@SinceKotlin("1.2") @InlineOnly public inline fun Double.pow(other: Int): Double = nativeMath.pow(this, other.toDouble()) @@ -370,6 +401,7 @@ public inline fun Double.pow(other: Int): Double = nativeMath.pow(this, other.to * * @see round */ +@SinceKotlin("1.2") @InlineOnly public inline fun Double.IEEErem(other: Double): Double = nativeMath.IEEEremainder(this, other) @@ -381,6 +413,7 @@ public inline fun Double.IEEErem(other: Double): Double = nativeMath.IEEEremaind * * @see abs function */ +@SinceKotlin("1.2") @InlineOnly public inline val Double.absoluteValue: Double get() = nativeMath.abs(this) @@ -393,6 +426,7 @@ public inline val Double.absoluteValue: Double get() = nativeMath.abs(this) * Special case: * - `NaN.sign` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline val Double.sign: Double get() = nativeMath.signum(this) @@ -401,11 +435,13 @@ public inline val Double.sign: Double get() = nativeMath.signum(this) * * If [sign] is `NaN` the sign of the result is undefined. */ +@SinceKotlin("1.2") @InlineOnly public inline fun Double.withSign(sign: Double): Double = nativeMath.copySign(this, sign) /** * Returns this value with the sign bit same as of the [sign] value. */ +@SinceKotlin("1.2") @InlineOnly public inline fun Double.withSign(sign: Int): Double = nativeMath.copySign(this, sign.toDouble()) @@ -419,17 +455,20 @@ public inline fun Double.withSign(sign: Int): Double = nativeMath.copySign(this, * - `x.ulp` is `+Inf` when `x` is `+Inf` or `-Inf` * - `0.0.ulp` is `Double.NIN_VALUE` */ +@SinceKotlin("1.2") @InlineOnly public inline val Double.ulp: Double get() = nativeMath.ulp(this) /** * Returns the [Double] value nearest to this value in direction of positive infinity. */ +@SinceKotlin("1.2") @InlineOnly public inline fun Double.nextUp(): Double = nativeMath.nextUp(this) /** * Returns the [Double] value nearest to this value in direction of negative infinity. */ +@SinceKotlin("1.2") @InlineOnly public inline fun Double.nextDown(): Double = nativeMath.nextAfter(this, Double.NEGATIVE_INFINITY) @@ -441,6 +480,7 @@ public inline fun Double.nextDown(): Double = nativeMath.nextAfter(this, Double. * - `x.nextTowards(x) == x` * */ +@SinceKotlin("1.2") @InlineOnly public inline fun Double.nextTowards(to: Double): Double = nativeMath.nextAfter(this, to) @@ -454,6 +494,7 @@ public inline fun Double.nextTowards(to: Double): Double = nativeMath.nextAfter( * * @throws IllegalArgumentException when this value is `NaN` */ +@SinceKotlin("1.2") public fun Double.roundToInt(): Int = when { isNaN() -> throw IllegalArgumentException("Cannot round NaN value.") this > Int.MAX_VALUE -> Int.MAX_VALUE @@ -471,6 +512,7 @@ public fun Double.roundToInt(): Int = when { * * @throws IllegalArgumentException when this value is `NaN` */ +@SinceKotlin("1.2") public fun Double.roundToLong(): Long = if (isNaN()) throw IllegalArgumentException("Cannot round NaN value.") else nativeMath.round(this) @@ -483,6 +525,7 @@ public fun Double.roundToLong(): Long = if (isNaN()) throw IllegalArgumentExcept * * - `sin(NaN|+Inf|-Inf)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun sin(a: Float): Float = nativeMath.sin(a.toDouble()).toFloat() @@ -492,6 +535,7 @@ public inline fun sin(a: Float): Float = nativeMath.sin(a.toDouble()).toFloat() * * - `cos(NaN|+Inf|-Inf)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun cos(a: Float): Float = nativeMath.cos(a.toDouble()).toFloat() @@ -501,6 +545,7 @@ public inline fun cos(a: Float): Float = nativeMath.cos(a.toDouble()).toFloat() * * - `tan(NaN|+Inf|-Inf)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun tan(a: Float): Float = nativeMath.tan(a.toDouble()).toFloat() @@ -511,6 +556,7 @@ public inline fun tan(a: Float): Float = nativeMath.tan(a.toDouble()).toFloat() * Special cases: * - `asin(v)` is `NaN`, when `abs(v) > 1` or v is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun asin(a: Float): Float = nativeMath.asin(a.toDouble()).toFloat() @@ -521,6 +567,7 @@ public inline fun asin(a: Float): Float = nativeMath.asin(a.toDouble()).toFloat( * Special cases: * - `acos(v)` is `NaN`, when `abs(v) > 1` or v is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun acos(a: Float): Float = nativeMath.acos(a.toDouble()).toFloat() @@ -531,6 +578,7 @@ public inline fun acos(a: Float): Float = nativeMath.acos(a.toDouble()).toFloat( * Special cases: * - `atan(NaN)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun atan(a: Float): Float = nativeMath.atan(a.toDouble()).toFloat() @@ -550,6 +598,7 @@ public inline fun atan(a: Float): Float = nativeMath.atan(a.toDouble()).toFloat( * - `atan2(-Inf, x)` is `-PI/2` for finite `x` * - `atan2(NaN, x)` and `atan2(y, NaN)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun atan2(y: Float, x: Float): Float = nativeMath.atan2(y.toDouble(), x.toDouble()).toFloat() @@ -562,6 +611,7 @@ public inline fun atan2(y: Float, x: Float): Float = nativeMath.atan2(y.toDouble * - `sinh(+Inf)` is `+Inf` * - `sinh(-Inf)` is `-Inf` */ +@SinceKotlin("1.2") @InlineOnly public inline fun sinh(a: Float): Float = nativeMath.sinh(a.toDouble()).toFloat() @@ -573,6 +623,7 @@ public inline fun sinh(a: Float): Float = nativeMath.sinh(a.toDouble()).toFloat( * - `cosh(NaN)` is `NaN` * - `cosh(+Inf|-Inf)` is `+Inf` */ +@SinceKotlin("1.2") @InlineOnly public inline fun cosh(a: Float): Float = nativeMath.cosh(a.toDouble()).toFloat() @@ -585,6 +636,7 @@ public inline fun cosh(a: Float): Float = nativeMath.cosh(a.toDouble()).toFloat( * - `tanh(+Inf)` is `1.0` * - `tanh(-Inf)` is `-1.0` */ +@SinceKotlin("1.2") @InlineOnly public inline fun tanh(a: Float): Float = nativeMath.tanh(a.toDouble()).toFloat() @@ -595,6 +647,7 @@ public inline fun tanh(a: Float): Float = nativeMath.tanh(a.toDouble()).toFloat( * - returns `+Inf` if any of arguments is infinite * - returns `NaN` if any of arguments is `NaN` and the other is not infinite */ +@SinceKotlin("1.2") @InlineOnly public inline fun hypot(x: Float, y: Float): Float = nativeMath.hypot(x.toDouble(), y.toDouble()).toFloat() @@ -604,6 +657,7 @@ public inline fun hypot(x: Float, y: Float): Float = nativeMath.hypot(x.toDouble * Special cases: * - `sqrt(x)` is `NaN` when `x < 0` or `x` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun sqrt(a: Float): Float = nativeMath.sqrt(a.toDouble()).toFloat() @@ -615,6 +669,7 @@ public inline fun sqrt(a: Float): Float = nativeMath.sqrt(a.toDouble()).toFloat( * - `exp(+Inf)` is `+Inf` * - `exp(-Inf)` is `0.0` */ +@SinceKotlin("1.2") @InlineOnly public inline fun exp(a: Float): Float = nativeMath.exp(a.toDouble()).toFloat() @@ -630,6 +685,7 @@ public inline fun exp(a: Float): Float = nativeMath.exp(a.toDouble()).toFloat() * * @see [exp] function. */ +@SinceKotlin("1.2") @InlineOnly public inline fun expm1(a: Float): Float = nativeMath.expm1(a.toDouble()).toFloat() @@ -643,6 +699,7 @@ public inline fun expm1(a: Float): Float = nativeMath.expm1(a.toDouble()).toFloa * - `log(+Inf, b)` is `+Inf` for `b > 1` and `-Inf` for `b < 1` * - `log(0.0, b)` is `-Inf` for `b > 1` and `+Inf` for `b > 1` */ +@SinceKotlin("1.2") public fun log(a: Float, base: Float): Float { if (base <= 0.0F || base == 1.0F) return Float.NaN return (nativeMath.log(a.toDouble()) / nativeMath.log(base.toDouble())).toFloat() @@ -657,6 +714,7 @@ public fun log(a: Float, base: Float): Float { * - `ln(+Inf)` is `+Inf` * - `ln(0.0)` is `-Inf` */ +@SinceKotlin("1.2") @InlineOnly public inline fun ln(a: Float): Float = nativeMath.log(a.toDouble()).toFloat() @@ -665,6 +723,7 @@ public inline fun ln(a: Float): Float = nativeMath.log(a.toDouble()).toFloat() * * @see [ln] function for special cases. */ +@SinceKotlin("1.2") @InlineOnly public inline fun log10(a: Float): Float = nativeMath.log10(a.toDouble()).toFloat() @@ -673,6 +732,7 @@ public inline fun log10(a: Float): Float = nativeMath.log10(a.toDouble()).toFloa * * @see [ln] function for special cases. */ +@SinceKotlin("1.2") public fun log2(a: Float): Float = (nativeMath.log(a.toDouble()) / LN2).toFloat() /** @@ -689,6 +749,7 @@ public fun log2(a: Float): Float = (nativeMath.log(a.toDouble()) / LN2).toFloat( * @see [ln] function * @see [expm1] function */ +@SinceKotlin("1.2") @InlineOnly public inline fun ln1p(a: Float): Float = nativeMath.log1p(a.toDouble()).toFloat() @@ -700,6 +761,7 @@ public inline fun ln1p(a: Float): Float = nativeMath.log1p(a.toDouble()).toFloat * Special cases: * - `ceil(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. */ +@SinceKotlin("1.2") @InlineOnly public inline fun ceil(a: Float): Float = nativeMath.ceil(a.toDouble()).toFloat() @@ -711,6 +773,7 @@ public inline fun ceil(a: Float): Float = nativeMath.ceil(a.toDouble()).toFloat( * Special cases: * - `floor(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. */ +@SinceKotlin("1.2") @InlineOnly public inline fun floor(a: Float): Float = nativeMath.floor(a.toDouble()).toFloat() @@ -722,6 +785,7 @@ public inline fun floor(a: Float): Float = nativeMath.floor(a.toDouble()).toFloa * Special cases: * - `truncate(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. */ +@SinceKotlin("1.2") public fun truncate(a: Float): Float = when { a.isNaN() || a.isInfinite() -> a a > 0 -> floor(a) @@ -734,6 +798,7 @@ public fun truncate(a: Float): Float = when { * Special cases: * - `round(x)` is `x` where `x` is `NaN` or `+Inf` or `-Inf` or already a mathematical integer. */ +@SinceKotlin("1.2") @InlineOnly public inline fun round(a: Float): Float = nativeMath.rint(a.toDouble()).toFloat() @@ -746,6 +811,7 @@ public inline fun round(a: Float): Float = nativeMath.rint(a.toDouble()).toFloat * * @see absoluteValue extension property for [Float] */ +@SinceKotlin("1.2") @InlineOnly public inline fun abs(a: Float): Float = nativeMath.abs(a) @@ -758,6 +824,7 @@ public inline fun abs(a: Float): Float = nativeMath.abs(a) * Special case: * - `sign(NaN)` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline fun sign(a: Float): Float = nativeMath.signum(a) @@ -768,6 +835,7 @@ public inline fun sign(a: Float): Float = nativeMath.signum(a) * * If either value is `NaN`, then the result is `NaN`. */ +@SinceKotlin("1.2") @InlineOnly public inline fun min(a: Float, b: Float): Float = nativeMath.min(a, b) /** @@ -775,6 +843,7 @@ public inline fun min(a: Float, b: Float): Float = nativeMath.min(a, b) * * If either value is `NaN`, then the result is `NaN`. */ +@SinceKotlin("1.2") @InlineOnly public inline fun max(a: Float, b: Float): Float = nativeMath.max(a, b) @@ -792,6 +861,7 @@ public inline fun max(a: Float, b: Float): Float = nativeMath.max(a, b) * - `x.pow(Inf)` is `NaN` for `abs(x) == 1.0` * - `x.pow(y)` is `NaN` for `x < 0` and `y` is finite and not an integer */ +@SinceKotlin("1.2") @InlineOnly public inline fun Float.pow(other: Float): Float = nativeMath.pow(this.toDouble(), other.toDouble()).toFloat() @@ -800,6 +870,7 @@ public inline fun Float.pow(other: Float): Float = nativeMath.pow(this.toDouble( * * See the other overload of [pow] for details. */ +@SinceKotlin("1.2") @InlineOnly public inline fun Float.pow(other: Int): Float = nativeMath.pow(this.toDouble(), other.toDouble()).toFloat() @@ -815,6 +886,7 @@ public inline fun Float.pow(other: Int): Float = nativeMath.pow(this.toDouble(), * * @see round */ +@SinceKotlin("1.2") @InlineOnly public inline fun Float.IEEErem(other: Float): Float = nativeMath.IEEEremainder(this.toDouble(), other.toDouble()).toFloat() @@ -826,6 +898,7 @@ public inline fun Float.IEEErem(other: Float): Float = nativeMath.IEEEremainder( * * @see abs function */ +@SinceKotlin("1.2") @InlineOnly public inline val Float.absoluteValue: Float get() = nativeMath.abs(this) @@ -838,6 +911,7 @@ public inline val Float.absoluteValue: Float get() = nativeMath.abs(this) * Special case: * - `NaN.sign` is `NaN` */ +@SinceKotlin("1.2") @InlineOnly public inline val Float.sign: Float get() = nativeMath.signum(this) @@ -846,11 +920,13 @@ public inline val Float.sign: Float get() = nativeMath.signum(this) * * If [sign] is `NaN` the sign of the result is undefined. */ +@SinceKotlin("1.2") @InlineOnly public inline fun Float.withSign(sign: Float): Float = nativeMath.copySign(this, sign) /** * Returns this value with the sign bit same as of the [sign] value. */ +@SinceKotlin("1.2") @InlineOnly public inline fun Float.withSign(sign: Int): Float = nativeMath.copySign(this, sign.toFloat()) @@ -864,17 +940,20 @@ public inline fun Float.withSign(sign: Int): Float = nativeMath.copySign(this, s * - `x.ulp` is `+Inf` when `x` is `+Inf` or `-Inf` * - `0.0.ulp` is `Float.NIN_VALUE` */ +@SinceKotlin("1.2") @InlineOnly public inline val Float.ulp: Float get() = nativeMath.ulp(this) /** * Returns the [Float] value nearest to this value in direction of positive infinity. */ +@SinceKotlin("1.2") @InlineOnly public inline fun Float.nextUp(): Float = nativeMath.nextUp(this) /** * Returns the [Float] value nearest to this value in direction of negative infinity. */ +@SinceKotlin("1.2") @InlineOnly public inline fun Float.nextDown(): Float = nativeMath.nextAfter(this, Double.NEGATIVE_INFINITY) @@ -886,6 +965,7 @@ public inline fun Float.nextDown(): Float = nativeMath.nextAfter(this, Double.NE * - `x.nextTowards(x) == x` * */ +@SinceKotlin("1.2") @InlineOnly public inline fun Float.nextTowards(to: Float): Float = nativeMath.nextAfter(this, to.toDouble()) @@ -899,6 +979,7 @@ public inline fun Float.nextTowards(to: Float): Float = nativeMath.nextAfter(thi * * @throws IllegalArgumentException when this value is `NaN` */ +@SinceKotlin("1.2") public fun Float.roundToInt(): Int = if (isNaN()) throw IllegalArgumentException("Cannot round NaN value.") else nativeMath.round(this) /** @@ -911,6 +992,7 @@ public fun Float.roundToInt(): Int = if (isNaN()) throw IllegalArgumentException * * @throws IllegalArgumentException when this value is `NaN` */ +@SinceKotlin("1.2") public fun Float.roundToLong(): Long = toDouble().roundToLong() @@ -925,18 +1007,21 @@ public fun Float.roundToLong(): Long = toDouble().roundToLong() * * @see absoluteValue extension property for [Int] */ +@SinceKotlin("1.2") @InlineOnly public inline fun abs(a: Int): Int = nativeMath.abs(a) /** * Returns the smaller of two values. */ +@SinceKotlin("1.2") @InlineOnly public inline fun min(a: Int, b: Int): Int = nativeMath.min(a, b) /** * Returns the greater of two values. */ +@SinceKotlin("1.2") @InlineOnly public inline fun max(a: Int, b: Int): Int = nativeMath.max(a, b) @@ -948,6 +1033,7 @@ public inline fun max(a: Int, b: Int): Int = nativeMath.max(a, b) * * @see abs function */ +@SinceKotlin("1.2") @InlineOnly public inline val Int.absoluteValue: Int get() = nativeMath.abs(this) @@ -957,6 +1043,7 @@ public inline val Int.absoluteValue: Int get() = nativeMath.abs(this) * - `0` if the value is zero, * - `1` if the value is positive */ +@SinceKotlin("1.2") public val Int.sign: Int get() = when { this < 0 -> -1 this > 0 -> 1 @@ -973,18 +1060,21 @@ public val Int.sign: Int get() = when { * * @see absoluteValue extension property for [Long] */ +@SinceKotlin("1.2") @InlineOnly public inline fun abs(a: Long): Long = nativeMath.abs(a) /** * Returns the smaller of two values. */ +@SinceKotlin("1.2") @InlineOnly public inline fun min(a: Long, b: Long): Long = nativeMath.min(a, b) /** * Returns the greater of two values. */ +@SinceKotlin("1.2") @InlineOnly public inline fun max(a: Long, b: Long): Long = nativeMath.max(a, b) @@ -996,6 +1086,7 @@ public inline fun max(a: Long, b: Long): Long = nativeMath.max(a, b) * * @see abs function */ +@SinceKotlin("1.2") @InlineOnly public inline val Long.absoluteValue: Long get() = nativeMath.abs(this) @@ -1005,6 +1096,7 @@ public inline val Long.absoluteValue: Long get() = nativeMath.abs(this) * - `0` if the value is zero, * - `1` if the value is positive */ +@SinceKotlin("1.2") public val Long.sign: Int get() = when { this < 0 -> -1 this > 0 -> 1 diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt index 57dfbf43e5e..fde47f04568 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt @@ -3013,6 +3013,23 @@ public abstract interface class kotlin/jvm/internal/markers/KMutableMap$Entry : public abstract interface class kotlin/jvm/internal/markers/KMutableSet : kotlin/jvm/internal/markers/KMutableCollection { } +public final class kotlin/math/MathKt { + public static final field E D + public static final field PI D + public static final fun getSign (I)I + public static final fun getSign (J)I + public static final fun log (DD)D + public static final fun log (FF)F + public static final fun log2 (D)D + public static final fun log2 (F)F + public static final fun roundToInt (D)I + public static final fun roundToInt (F)I + public static final fun roundToLong (D)J + public static final fun roundToLong (F)J + public static final fun truncate (D)D + public static final fun truncate (F)F +} + public final class kotlin/properties/Delegates { public static final field INSTANCE Lkotlin/properties/Delegates; public final fun notNull ()Lkotlin/properties/ReadWriteProperty; diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib.txt index 269d28c83ff..dbde2a73a23 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib.txt @@ -2021,6 +2021,23 @@ public final class kotlin/io/TextStreamsKt { public static final fun useLines (Ljava/io/Reader;Lkotlin/jvm/functions/Function1;)Ljava/lang/Object; } +public final class kotlin/math/MathKt { + public static final field E D + public static final field PI D + public static final fun getSign (I)I + public static final fun getSign (J)I + public static final fun log (DD)D + public static final fun log (FF)F + public static final fun log2 (D)D + public static final fun log2 (F)F + public static final fun roundToInt (D)I + public static final fun roundToInt (F)I + public static final fun roundToLong (D)J + public static final fun roundToLong (F)J + public static final fun truncate (D)D + public static final fun truncate (F)F +} + public final class kotlin/properties/Delegates { public static final field INSTANCE Lkotlin/properties/Delegates; public final fun notNull ()Lkotlin/properties/ReadWriteProperty;