Skip to content

Commit 2c2ec4c

Browse files
committed
Fix writing -0.0f and -0.0 values as 0.0
1 parent 0850bc9 commit 2c2ec4c

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

jsoniter-scala-core/js/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonWriter.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,9 +2514,10 @@ final class JsonWriter private[jsoniter_scala](
25142514
// "The Schubfach way to render doubles": https://drive.google.com/file/d/1luHhyQF9zKlM8yJ1nebU0OgVYhfC6CBN/view
25152515
// Sources with the license are here: https://github.com/c4f7fcce9cb06515/Schubfach/blob/3c92d3c9b1fead540616c918cdfef432bca53dfa/todec/src/math/FloatToDecimal.java
25162516
private[this] def writeFloat(x: Float): Unit = {
2517+
val bits = java.lang.Float.floatToIntBits(x)
25172518
var pos = ensureBufCapacity(15)
25182519
val buf = this.buf
2519-
if (x < 0.0f) {
2520+
if (bits < 0) {
25202521
buf(pos) = '-'
25212522
pos += 1
25222523
}
@@ -2526,7 +2527,6 @@ final class JsonWriter private[jsoniter_scala](
25262527
buf(pos + 2) = '0'
25272528
pos += 3
25282529
} else {
2529-
val bits = java.lang.Float.floatToIntBits(x)
25302530
var e2 = (bits >> 23 & 0xFF) - 150
25312531
var m2 = bits & 0x7FFFFF | 0x800000
25322532
var m10, e10 = 0
@@ -2635,9 +2635,10 @@ final class JsonWriter private[jsoniter_scala](
26352635
// "The Schubfach way to render doubles": https://drive.google.com/file/d/1luHhyQF9zKlM8yJ1nebU0OgVYhfC6CBN/view
26362636
// Sources with the license are here: https://github.com/c4f7fcce9cb06515/Schubfach/blob/3c92d3c9b1fead540616c918cdfef432bca53dfa/todec/src/math/DoubleToDecimal.java
26372637
private[this] def writeDouble(x: Double): Unit = {
2638+
val bits = java.lang.Double.doubleToLongBits(x)
26382639
var pos = ensureBufCapacity(24)
26392640
val buf = this.buf
2640-
if (x < 0.0) {
2641+
if (bits < 0L) {
26412642
buf(pos) = '-'
26422643
pos += 1
26432644
}
@@ -2647,7 +2648,6 @@ final class JsonWriter private[jsoniter_scala](
26472648
buf(pos + 2) = '0'
26482649
pos += 3
26492650
} else {
2650-
val bits = java.lang.Double.doubleToLongBits(x)
26512651
var e2 = ((bits >>> 52).toInt & 0x7FF) - 1075
26522652
var m2 = bits & 0xFFFFFFFFFFFFFL | 0x10000000000000L
26532653
var m10 = 0L

jsoniter-scala-core/jvm/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonWriter.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,17 +2250,17 @@ final class JsonWriter private[jsoniter_scala](
22502250
// "The Schubfach way to render doubles": https://drive.google.com/file/d/1luHhyQF9zKlM8yJ1nebU0OgVYhfC6CBN/view
22512251
// Sources with the license are here: https://github.com/c4f7fcce9cb06515/Schubfach/blob/3c92d3c9b1fead540616c918cdfef432bca53dfa/todec/src/math/FloatToDecimal.java
22522252
private[this] def writeFloat(x: Float): Unit = {
2253+
val bits = java.lang.Float.floatToRawIntBits(x)
22532254
var pos = ensureBufCapacity(15)
22542255
val buf = this.buf
2255-
if (x < 0.0f) {
2256+
if (bits < 0) {
22562257
buf(pos) = '-'
22572258
pos += 1
22582259
}
2259-
if (x == 0.0f) {
2260+
if (bits << 1 == 0) {
22602261
ByteArrayAccess.setInt(buf, pos, 0x302E30)
22612262
pos += 3
22622263
} else {
2263-
val bits = java.lang.Float.floatToRawIntBits(x)
22642264
var e2 = (bits >> 23 & 0xFF) - 150
22652265
var m2 = bits & 0x7FFFFF | 0x800000
22662266
var m10, e10 = 0
@@ -2362,17 +2362,17 @@ final class JsonWriter private[jsoniter_scala](
23622362
// "The Schubfach way to render doubles": https://drive.google.com/file/d/1luHhyQF9zKlM8yJ1nebU0OgVYhfC6CBN/view
23632363
// Sources with the license are here: https://github.com/c4f7fcce9cb06515/Schubfach/blob/3c92d3c9b1fead540616c918cdfef432bca53dfa/todec/src/math/DoubleToDecimal.java
23642364
private[this] def writeDouble(x: Double): Unit = {
2365+
val bits = java.lang.Double.doubleToRawLongBits(x)
23652366
var pos = ensureBufCapacity(25) // -1.2898455142673966E-135.toString.length + 1
23662367
val buf = this.buf
2367-
if (x < 0.0) {
2368+
if (bits < 0L) {
23682369
buf(pos) = '-'
23692370
pos += 1
23702371
}
2371-
if (x == 0.0) {
2372+
if (bits << 1 == 0L) {
23722373
ByteArrayAccess.setInt(buf, pos, 0x302E30)
23732374
pos += 3
23742375
} else {
2375-
val bits = java.lang.Double.doubleToRawLongBits(x)
23762376
var e2 = ((bits >> 52).toInt & 0x7FF) - 1075
23772377
var m2 = bits & 0xFFFFFFFFFFFFFL | 0x10000000000000L
23782378
var m10 = 0L

jsoniter-scala-core/native/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonWriter.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,17 +2250,17 @@ final class JsonWriter private[jsoniter_scala](
22502250
// "The Schubfach way to render doubles": https://drive.google.com/file/d/1luHhyQF9zKlM8yJ1nebU0OgVYhfC6CBN/view
22512251
// Sources with the license are here: https://github.com/c4f7fcce9cb06515/Schubfach/blob/3c92d3c9b1fead540616c918cdfef432bca53dfa/todec/src/math/FloatToDecimal.java
22522252
private[this] def writeFloat(x: Float): Unit = {
2253+
val bits = java.lang.Float.floatToRawIntBits(x)
22532254
var pos = ensureBufCapacity(15)
22542255
val buf = this.buf
2255-
if (x < 0.0f) {
2256+
if (bits < 0) {
22562257
buf(pos) = '-'
22572258
pos += 1
22582259
}
2259-
if (x == 0.0f) {
2260+
if (bits << 1 == 0) {
22602261
ByteArrayAccess.setInt(buf, pos, 0x302E30)
22612262
pos += 3
22622263
} else {
2263-
val bits = java.lang.Float.floatToRawIntBits(x)
22642264
var e2 = (bits >> 23 & 0xFF) - 150
22652265
var m2 = bits & 0x7FFFFF | 0x800000
22662266
var m10, e10 = 0
@@ -2362,17 +2362,17 @@ final class JsonWriter private[jsoniter_scala](
23622362
// "The Schubfach way to render doubles": https://drive.google.com/file/d/1luHhyQF9zKlM8yJ1nebU0OgVYhfC6CBN/view
23632363
// Sources with the license are here: https://github.com/c4f7fcce9cb06515/Schubfach/blob/3c92d3c9b1fead540616c918cdfef432bca53dfa/todec/src/math/DoubleToDecimal.java
23642364
private[this] def writeDouble(x: Double): Unit = {
2365+
val bits = java.lang.Double.doubleToRawLongBits(x)
23652366
var pos = ensureBufCapacity(25) // -1.2898455142673966E-135.toString.length + 1
23662367
val buf = this.buf
2367-
if (x < 0.0) {
2368+
if (bits < 0L) {
23682369
buf(pos) = '-'
23692370
pos += 1
23702371
}
2371-
if (x == 0.0) {
2372+
if (bits << 1 == 0L) {
23722373
ByteArrayAccess.setInt(buf, pos, 0x302E30)
23732374
pos += 3
23742375
} else {
2375-
val bits = java.lang.Double.doubleToRawLongBits(x)
23762376
var e2 = ((bits >> 52).toInt & 0x7FF) - 1075
23772377
var m2 = bits & 0xFFFFFFFFFFFFFL | 0x10000000000000L
23782378
var m10 = 0L

jsoniter-scala-core/shared/src/test/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonWriterSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ class JsonWriterSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyCh
504504
val s = withWriter(_.writeVal(n))
505505
val l = s.length
506506
val i = s.indexOf('.')
507-
s.toFloat shouldBe n // no data loss when parsing by JVM, Native or JS Platform
507+
java.lang.Float.floatToIntBits(s.toFloat) shouldBe java.lang.Float.floatToIntBits(n) // no data loss when parsing by JVM, Native or JS Platform
508508
l should be <= es.length + {
509509
if (TestUtils.isJS) {
510510
if (es.indexOf('.') < 0) 3 // formatting differs from JS for floats represented as whole numbers
@@ -621,7 +621,7 @@ class JsonWriterSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyCh
621621
val s = withWriter(_.writeVal(n))
622622
val l = s.length
623623
val i = s.indexOf('.')
624-
s.toDouble shouldBe n // no data loss when parsing by JVM, Native or JS Platform
624+
java.lang.Double.doubleToLongBits(s.toDouble) shouldBe java.lang.Double.doubleToLongBits(n) // no data loss when parsing by JVM, Native or JS Platform
625625
l should be <= es.length + {
626626
if (TestUtils.isJS) {
627627
if (es.indexOf('.') < 0) 4 // formatting differs from JS for doubles represented as whole numbers

0 commit comments

Comments
 (0)