Provide Double and Float bit conversion functions as extensions

Instance extension: Double/Float.toBits/toRawBits
Companion extension: Double/Float.Companion.fromBits

 #KT-18264 Fixed
This commit is contained in:
Ilya Gorbunov
2017-07-20 00:55:55 +03:00
parent 6373ac7ef0
commit 7e48f8b180
2 changed files with 72 additions and 0 deletions
@@ -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)