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:
@@ -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())
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user