Skip to content

Commit 7427374

Browse files
authored
More test cases for rounding during serialization of floats and doubles (plokhotnyuk#502)
1 parent a693a94 commit 7427374

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

jsoniter-scala-core/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
@@ -1865,10 +1865,10 @@ final class JsonWriter private[jsoniter_scala](
18651865
}) {
18661866
sv |= pv
18671867
sm |= pm
1868-
dp = newDp
1869-
dm = newDm
18701868
pv = dv * 3435973837L
18711869
dv = (pv >> 35).toInt // divide positive int by 10
1870+
dp = newDp
1871+
dm = newDm
18721872
len -= 1
18731873
}
18741874
if ((sm & 0x780000000L) != 0) dmIsTrailingZeros = false // test if all remainders of divisions by 10 are zeros
@@ -1881,7 +1881,7 @@ final class JsonWriter private[jsoniter_scala](
18811881
len -= 1
18821882
}
18831883
}
1884-
pv &= 0x780000000L // get the last removed digit
1884+
pv &= 0x780000000L // mask the last removed digit
18851885
if (!(dvIsTrailingZeros && pv == 0x400000000L /* is 5 */ && (dv & 0x1) == 0 ||
18861886
(pv < 0x400000000L /* is less than 5 */ && (dv != dm || dmIsTrailingZeros)))) dv += 1
18871887
} else {
@@ -1893,8 +1893,8 @@ final class JsonWriter private[jsoniter_scala](
18931893
}) {
18941894
pv = dv * 1374389535L
18951895
dv = (pv >> 37).toInt // divide positive int by 100
1896-
dm = newDm
18971896
dp = newDp
1897+
dm = newDm
18981898
len -= 2
18991899
}
19001900
val roundUp =
@@ -2060,12 +2060,12 @@ final class JsonWriter private[jsoniter_scala](
20602060
newDp > newDm
20612061
}) {
20622062
if (dmIsTrailingZeros) dmIsTrailingZeros = newDm * 10 == dm
2063-
dp = newDp
2064-
dm = newDm
20652063
dvIsTrailingZeros &= lastRemovedDigit == 0
20662064
val newDv = dv / 10
20672065
lastRemovedDigit = dv - newDv * 10
20682066
dv = newDv
2067+
dp = newDp
2068+
dm = newDm
20692069
len -= 1
20702070
}
20712071
if (dmIsTrailingZeros) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@ class JsonWriterSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyCh
546546
check(1.00014165E-36f)
547547
check(200f)
548548
check(3.3554432E7f)
549+
check(1.26217745E-29f)
549550
forAll(arbitrary[Int], minSuccessful(10000))(n => check(n.toFloat))
550551
forAll(arbitrary[Int], minSuccessful(10000)) { n =>
551552
val x = java.lang.Float.intBitsToFloat(n)
@@ -624,6 +625,7 @@ class JsonWriterSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyCh
624625
check(1.9430376160308388E16)
625626
check(-6.9741824662760956E19)
626627
check(4.3816050601147837E18)
628+
check(7.1202363472230444E-307)
627629
forAll(arbitrary[Long], minSuccessful(10000))(n => check(n.toDouble))
628630
forAll(arbitrary[Long], minSuccessful(10000)) { n =>
629631
val x = java.lang.Double.longBitsToDouble(n)

0 commit comments

Comments
 (0)