diff --git a/libraries/stdlib/src/kotlin/util/Numbers.kt b/libraries/stdlib/src/kotlin/util/Numbers.kt index 1587dd4c498..ebd79090e8c 100644 --- a/libraries/stdlib/src/kotlin/util/Numbers.kt +++ b/libraries/stdlib/src/kotlin/util/Numbers.kt @@ -40,3 +40,51 @@ public inline fun Double.isFinite(): Boolean = !isInfinite() && !isNaN() */ @kotlin.internal.InlineOnly public inline 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") +@kotlin.internal.InlineOnly +public inline fun Double.toBits(): Long = java.lang.Double.doubleToLongBits(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") +@kotlin.internal.InlineOnly +public inline fun Double.toRawBits(): Long = java.lang.Double.doubleToRawLongBits(this) + +/** + * Returns the [Double] value corresponding to a given bit representation. + */ +@SinceKotlin("1.2") +@kotlin.internal.InlineOnly +public inline fun Double.Companion.fromBits(bits: Long) = java.lang.Double.longBitsToDouble(bits) + +/** + * Returns a bit representation of the specified floating-point value as [Int] + * according to the IEEE 754 floating-point "single format" bit layout. + */ +@SinceKotlin("1.2") +@kotlin.internal.InlineOnly +public inline fun Float.toBits(): Int = java.lang.Float.floatToIntBits(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. + */ +@SinceKotlin("1.2") +@kotlin.internal.InlineOnly +public inline fun Float.toRawBits(): Int = java.lang.Float.floatToRawIntBits(this) + +/** + * Returns the [Float] value corresponding to a given bit representation. + */ +@SinceKotlin("1.2") +@kotlin.internal.InlineOnly +public inline fun Float.Companion.fromBits(bits: Int): Float = java.lang.Float.intBitsToFloat(bits) diff --git a/libraries/stdlib/test/numbers/NumbersJVMTest.kt b/libraries/stdlib/test/numbers/NumbersJVMTest.kt index 1d38b8ab9dd..6dcfeaa5f4b 100644 --- a/libraries/stdlib/test/numbers/NumbersJVMTest.kt +++ b/libraries/stdlib/test/numbers/NumbersJVMTest.kt @@ -41,5 +41,29 @@ class NumbersJVMTest { assertEquals(java.lang.Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY) } + @Test fun doubleToBits() { + assertEquals(0x400921fb54442d18L, kotlin.math.PI.toBits()) + assertEquals(kotlin.math.PI, Double.fromBits(0x400921fb54442d18L)) + + for (value in listOf(Double.NEGATIVE_INFINITY, -Double.MAX_VALUE, -1.0, -Double.MIN_VALUE, -0.0, 0.0, Double.POSITIVE_INFINITY, Double.MAX_VALUE, 1.0, Double.MIN_VALUE)) { + assertEquals(value, Double.fromBits(value.toBits())) + assertEquals(value, Double.fromBits(value.toRawBits())) + } + assertTrue(Double.NaN.toBits().let(Double.Companion::fromBits).isNaN()) + assertTrue(Double.NaN.toRawBits().let { Double.fromBits(it) }.isNaN()) + } + + @Test fun floatToBits() { + val PI_F = kotlin.math.PI.toFloat() + assertEquals(0x40490fdb, PI_F.toBits()) + assertEquals(PI_F, Float.fromBits(0x40490fdb)) + + for (value in listOf(Float.NEGATIVE_INFINITY, -Float.MAX_VALUE, -1.0F, -Float.MIN_VALUE, -0.0F, 0.0F, Float.POSITIVE_INFINITY, Float.MAX_VALUE, 1.0F, Float.MIN_VALUE)) { + assertEquals(value, Float.fromBits(value.toBits())) + assertEquals(value, Float.fromBits(value.toRawBits())) + } + assertTrue(Float.NaN.toBits().let(Float.Companion::fromBits).isNaN()) + assertTrue(Float.NaN.toRawBits().let { Float.fromBits(it) }.isNaN()) + } } \ No newline at end of file