From 08afa59dcc42c5e8dd430f4785a1a4f5e5721dc8 Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Thu, 10 Dec 2020 19:17:04 +0700 Subject: [PATCH] Make Float values in Memory be actually stored with Double --- .../space/kscience/kmath/memory/Memory.kt | 4 ++++ .../kscience/kmath/memory/MemoryTest.kt | 23 ++++++++----------- .../kscience/kmath/memory/DataViewMemory.kt | 4 ++-- .../kscience/kmath/memory/ByteBufferMemory.kt | 4 ++-- .../kscience/kmath/memory/NativeMemory.kt | 4 ++-- 5 files changed, 19 insertions(+), 20 deletions(-) diff --git a/kmath-memory/src/commonMain/kotlin/space/kscience/kmath/memory/Memory.kt b/kmath-memory/src/commonMain/kotlin/space/kscience/kmath/memory/Memory.kt index 930b21095..c784575a1 100644 --- a/kmath-memory/src/commonMain/kotlin/space/kscience/kmath/memory/Memory.kt +++ b/kmath-memory/src/commonMain/kotlin/space/kscience/kmath/memory/Memory.kt @@ -56,6 +56,8 @@ public interface MemoryReader { /** * Reads [Float] at certain [offset]. + * + * **Warning: since JS can't handle Float properly, the size of segment read by this function is 8 bytes.** */ public fun readFloat(offset: Int): Float @@ -112,6 +114,8 @@ public interface MemoryWriter { /** * Writes [Float] at certain [offset]. + * + * **Warning: since JS can't handle Float properly, the size of segment written by this function is 8 bytes.** */ public fun writeFloat(offset: Int, value: Float) diff --git a/kmath-memory/src/commonTest/kotlin/kscience/kmath/memory/MemoryTest.kt b/kmath-memory/src/commonTest/kotlin/kscience/kmath/memory/MemoryTest.kt index a828d7e9d..252f5c700 100644 --- a/kmath-memory/src/commonTest/kotlin/kscience/kmath/memory/MemoryTest.kt +++ b/kmath-memory/src/commonTest/kotlin/kscience/kmath/memory/MemoryTest.kt @@ -54,20 +54,15 @@ internal class MemoryTest { fun rwFloat() { val mem = Memory.allocate(64) mem.write { writeFloat(0, 12.12345f) } - println(1) - assertEquals(12.12345027923584, mem.read { readFloat(0) }.toDouble()) - mem.write { writeFloat(4, -313.13f) } - println(2) - assertEquals(-313.1300048828125, mem.read { readFloat(4) }.toDouble()) - mem.write { writeFloat(8, Float.NaN) } - println(3) - assertEquals(Float.NaN, mem.read { readFloat(8) }) - mem.write { writeFloat(12, Float.POSITIVE_INFINITY) } - println(4) - assertEquals(Float.POSITIVE_INFINITY, mem.read { readFloat(12) }) - mem.write { writeFloat(12, Float.NEGATIVE_INFINITY) } - println(5) - assertEquals(Float.NEGATIVE_INFINITY, mem.read { readFloat(12) }) + assertEquals(12.12345f, mem.read { readFloat(0) }) + mem.write { writeFloat(8, -313.13f) } + assertEquals(-313.13f, mem.read { readFloat(8) }) + mem.write { writeFloat(16, Float.NaN) } + assertEquals(Float.NaN, mem.read { readFloat(16) }) + mem.write { writeFloat(24, Float.POSITIVE_INFINITY) } + assertEquals(Float.POSITIVE_INFINITY, mem.read { readFloat(24) }) + mem.write { writeFloat(32, Float.NEGATIVE_INFINITY) } + assertEquals(Float.NEGATIVE_INFINITY, mem.read { readFloat(32) }) } @Test diff --git a/kmath-memory/src/jsMain/kotlin/space/kscience/kmath/memory/DataViewMemory.kt b/kmath-memory/src/jsMain/kotlin/space/kscience/kmath/memory/DataViewMemory.kt index 6c9d153b5..8ac97b759 100644 --- a/kmath-memory/src/jsMain/kotlin/space/kscience/kmath/memory/DataViewMemory.kt +++ b/kmath-memory/src/jsMain/kotlin/space/kscience/kmath/memory/DataViewMemory.kt @@ -30,7 +30,7 @@ private class DataViewMemory(val view: DataView) : Memory { override fun readDouble(offset: Int): Double = view.getFloat64(offset, true) - override fun readFloat(offset: Int): Float = view.getFloat32(offset, true) + override fun readFloat(offset: Int): Float = view.getFloat64(offset, true).toFloat() override fun readByte(offset: Int): Byte = view.getInt8(offset) @@ -56,7 +56,7 @@ private class DataViewMemory(val view: DataView) : Memory { } override fun writeFloat(offset: Int, value: Float) { - view.setFloat32(offset, value, true) + view.setFloat64(offset, value.toDouble(), true) } override fun writeByte(offset: Int, value: Byte) { diff --git a/kmath-memory/src/jvmMain/kotlin/space/kscience/kmath/memory/ByteBufferMemory.kt b/kmath-memory/src/jvmMain/kotlin/space/kscience/kmath/memory/ByteBufferMemory.kt index 2d8bb600c..41a426ea8 100644 --- a/kmath-memory/src/jvmMain/kotlin/space/kscience/kmath/memory/ByteBufferMemory.kt +++ b/kmath-memory/src/jvmMain/kotlin/space/kscience/kmath/memory/ByteBufferMemory.kt @@ -44,7 +44,7 @@ internal class ByteBufferMemory( override fun readDouble(offset: Int) = buffer.getDouble(position(offset)) - override fun readFloat(offset: Int) = buffer.getFloat(position(offset)) + override fun readFloat(offset: Int) = buffer.getDouble(position(offset)).toFloat() override fun readByte(offset: Int) = buffer.get(position(offset)) @@ -69,7 +69,7 @@ internal class ByteBufferMemory( } override fun writeFloat(offset: Int, value: Float) { - buffer.putFloat(position(offset), value) + buffer.putDouble(position(offset), value.toDouble()) } override fun writeByte(offset: Int, value: Byte) { diff --git a/kmath-memory/src/nativeMain/kotlin/space/kscience/kmath/memory/NativeMemory.kt b/kmath-memory/src/nativeMain/kotlin/space/kscience/kmath/memory/NativeMemory.kt index 97f503622..952ce5896 100644 --- a/kmath-memory/src/nativeMain/kotlin/space/kscience/kmath/memory/NativeMemory.kt +++ b/kmath-memory/src/nativeMain/kotlin/space/kscience/kmath/memory/NativeMemory.kt @@ -31,7 +31,7 @@ internal class NativeMemory( override fun readDouble(offset: Int) = array.getDoubleAt(position(offset)) - override fun readFloat(offset: Int) = array.getFloatAt(position(offset)) + override fun readFloat(offset: Int) = array.getDoubleAt(position(offset)).toFloat() override fun readByte(offset: Int) = array[position(offset)] @@ -56,7 +56,7 @@ internal class NativeMemory( } override fun writeFloat(offset: Int, value: Float) { - array.setFloatAt(position(offset), value) + array.setDoubleAt(position(offset), value.toDouble()) } override fun writeByte(offset: Int, value: Byte) {