Skip to content

Commit aec58e6

Browse files
FunnycrabFunnycrab
Funnycrab
authored and
Funnycrab
committed
BUG: Fix rollover handling in json encoding
This is a fix attempt for issue #15716 as well as #15864. Note that whenever the frac is incremented, there is a chance that its value may hit the value of pow10.
1 parent d1e1ba0 commit aec58e6

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

pandas/_libs/src/ujson/lib/ultrajsonenc.c

+6-5
Original file line numberDiff line numberDiff line change
@@ -823,17 +823,18 @@ int Buffer_AppendDoubleUnchecked(JSOBJ obj, JSONObjectEncoder *enc,
823823

824824
if (diff > 0.5) {
825825
++frac;
826-
/* handle rollover, e.g. case 0.99 with prec 1 is 1.0 */
827-
if (frac >= pow10) {
828-
frac = 0;
829-
++whole;
830-
}
831826
} else if (diff == 0.5 && ((frac == 0) || (frac & 1))) {
832827
/* if halfway, round up if odd, OR
833828
if last digit is 0. That last part is strange */
834829
++frac;
835830
}
836831

832+
/* handle rollover, e.g. case 0.99 with prec 1 is 1.0 and case 0.95 with prec is 1.0 as well */
833+
if (frac >= pow10) {
834+
frac = 0;
835+
++whole;
836+
}
837+
837838
if (enc->doublePrecision == 0) {
838839
diff = value - whole;
839840

0 commit comments

Comments
 (0)