diff --git a/libraries/stdlib/js/src/kotlin/NumbersJs.kt b/libraries/stdlib/js/src/kotlin/NumbersJs.kt index 0e0c2397453..77679dd482d 100644 --- a/libraries/stdlib/js/src/kotlin/NumbersJs.kt +++ b/libraries/stdlib/js/src/kotlin/NumbersJs.kt @@ -38,6 +38,63 @@ public actual fun Double.isFinite(): Boolean = !isInfinite() && !isNaN() public actual fun Float.isFinite(): Boolean = !isInfinite() && !isNaN() +/** + * Returns a bit representation of the specified floating-point value as [Long] + * according to the IEEE 754 floating-point "double format" bit layout. + */ +@SinceKotlin("1.2") +public actual fun Double.toBits(): Long = + doubleToRawBits(if (this.isNaN()) Double.NaN else this) + +/** + * Returns a bit representation of the specified floating-point value as [Long] + * according to the IEEE 754 floating-point "double format" bit layout, + * preserving `NaN` values exact layout. + */ +@SinceKotlin("1.2") +public actual fun Double.toRawBits(): Long = + doubleToRawBits(this) + +/** + * Returns the [Double] value corresponding to a given bit representation. + */ +@SinceKotlin("1.2") +@kotlin.internal.InlineOnly +public actual inline fun Double.Companion.fromBits(bits: Long): Double = + doubleFromBits(bits) + +/** + * Returns a bit representation of the specified floating-point value as [Int] + * according to the IEEE 754 floating-point "single format" bit layout. + * + * Note that in Kotlin/JS [Float] range is wider than "single format" bit layout can represent, + * so some [Float] values may overflow, underflow or loose their accuracy after conversion to bits and back. + */ +@SinceKotlin("1.2") +public actual fun Float.toBits(): Int = + floatToRawBits(if (this.isNaN()) Float.NaN else this) + +/** + * Returns a bit representation of the specified floating-point value as [Int] + * according to the IEEE 754 floating-point "single format" bit layout, + * preserving `NaN` values exact layout. + * + * Note that in Kotlin/JS [Float] range is wider than "single format" bit layout can represent, + * so some [Float] values may overflow, underflow or loose their accuracy after conversion to bits and back. + */ +@SinceKotlin("1.2") +public actual fun Float.toRawBits(): Int = + floatToRawBits(this) + +/** + * Returns the [Float] value corresponding to a given bit representation. + */ +@SinceKotlin("1.2") +@kotlin.internal.InlineOnly +public actual inline fun Float.Companion.fromBits(bits: Int): Float = + floatFromBits(bits) + + /** * Counts the number of set bits in the binary representation of this [Int] number. */ diff --git a/libraries/stdlib/js/src/kotlin/math.kt b/libraries/stdlib/js/src/kotlin/math.kt index 5bfcf44ac2a..96067b5f025 100644 --- a/libraries/stdlib/js/src/kotlin/math.kt +++ b/libraries/stdlib/js/src/kotlin/math.kt @@ -448,6 +448,18 @@ public actual inline val Double.absoluteValue: Double get() = nativeMath.abs(thi @InlineOnly public actual inline val Double.sign: Double get() = nativeSign(this) +/** + * 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 actual fun Double.withSign(sign: Double): Double { + val thisSignBit = doubleSignBit(this) + val newSignBit = doubleSignBit(sign) + return if (thisSignBit == newSignBit) this else -this +} + /** * Returns this value with the sign bit same as of the [sign] value. */ diff --git a/libraries/stdlib/js/src/kotlin/math_js-ir.kt b/libraries/stdlib/js/src/kotlin/math_js-ir.kt deleted file mode 100644 index 69456f6b51d..00000000000 --- a/libraries/stdlib/js/src/kotlin/math_js-ir.kt +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ -package kotlin.math - -/** - * 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 actual fun Double.withSign(sign: Double): Double { - val thisSignBit = doubleSignBit(this) - val newSignBit = doubleSignBit(sign) - return if (thisSignBit == newSignBit) this else -this -} \ No newline at end of file diff --git a/libraries/stdlib/js/src/kotlin/numbers_js-ir.kt b/libraries/stdlib/js/src/kotlin/numbers_js-ir.kt deleted file mode 100644 index 771a4b9585f..00000000000 --- a/libraries/stdlib/js/src/kotlin/numbers_js-ir.kt +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package kotlin - -/** - * Returns a bit representation of the specified floating-point value as [Long] - * according to the IEEE 754 floating-point "double format" bit layout. - */ -@SinceKotlin("1.2") -public actual fun Double.toBits(): Long = - doubleToRawBits(if (this.isNaN()) Double.NaN else this) - -/** - * Returns a bit representation of the specified floating-point value as [Long] - * according to the IEEE 754 floating-point "double format" bit layout, - * preserving `NaN` values exact layout. - */ -@SinceKotlin("1.2") -public actual fun Double.toRawBits(): Long = - doubleToRawBits(this) - -/** - * Returns the [Double] value corresponding to a given bit representation. - */ -@SinceKotlin("1.2") -@kotlin.internal.InlineOnly -public actual inline fun Double.Companion.fromBits(bits: Long): Double = - doubleFromBits(bits) - -/** - * Returns a bit representation of the specified floating-point value as [Int] - * according to the IEEE 754 floating-point "single format" bit layout. - * - * Note that in Kotlin/JS [Float] range is wider than "single format" bit layout can represent, - * so some [Float] values may overflow, underflow or loose their accuracy after conversion to bits and back. - */ -@SinceKotlin("1.2") -public actual fun Float.toBits(): Int = - floatToRawBits(if (this.isNaN()) Float.NaN else this) - -/** - * Returns a bit representation of the specified floating-point value as [Int] - * according to the IEEE 754 floating-point "single format" bit layout, - * preserving `NaN` values exact layout. - * - * Note that in Kotlin/JS [Float] range is wider than "single format" bit layout can represent, - * so some [Float] values may overflow, underflow or loose their accuracy after conversion to bits and back. - */ -@SinceKotlin("1.2") -public actual fun Float.toRawBits(): Int = - floatToRawBits(this) - -/** - * Returns the [Float] value corresponding to a given bit representation. - */ -@SinceKotlin("1.2") -@kotlin.internal.InlineOnly -public actual inline fun Float.Companion.fromBits(bits: Int): Float = - floatFromBits(bits) \ No newline at end of file diff --git a/libraries/stdlib/js/src/kotlin/text/numberConversions.kt b/libraries/stdlib/js/src/kotlin/text/numberConversions.kt index 157cf1fe941..28714521963 100644 --- a/libraries/stdlib/js/src/kotlin/text/numberConversions.kt +++ b/libraries/stdlib/js/src/kotlin/text/numberConversions.kt @@ -124,6 +124,15 @@ public actual inline fun Short.toString(radix: Int): String = this.toInt().toStr @SinceKotlin("1.2") public actual fun Int.toString(radix: Int): String = asDynamic().toString(checkRadix(radix)) +/** + * Returns a string representation of this [Long] value in the specified [radix]. + * + * @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion. + */ +@SinceKotlin("1.2") +public actual fun Long.toString(radix: Int): String = + this.toStringImpl(checkRadix(radix)) + private fun String.isNaN(): Boolean = when (this.lowercase()) { "nan", "+nan", "-nan" -> true else -> false diff --git a/libraries/stdlib/js/src/kotlin/text/numberConversions_js-ir.kt b/libraries/stdlib/js/src/kotlin/text/numberConversions_js-ir.kt deleted file mode 100644 index 5a17b444518..00000000000 --- a/libraries/stdlib/js/src/kotlin/text/numberConversions_js-ir.kt +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package kotlin.text - -/** - * Returns a string representation of this [Long] value in the specified [radix]. - * - * @throws IllegalArgumentException when [radix] is not a valid radix for number to string conversion. - */ -@SinceKotlin("1.2") -public actual fun Long.toString(radix: Int): String = - this.toStringImpl(checkRadix(radix)) \ No newline at end of file