Skip to content

Commit 28c59ab

Browse files
authored
Merge pull request Tencent#1922 from jack-perisich/grisu2_precision
Fix small errors in dtoa output for certain doubles
2 parents 48fbd8c + bb06211 commit 28c59ab

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

include/rapidjson/internal/dtoa.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ inline int CountDecimalDigit32(uint32_t n) {
5858
}
5959

6060
inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buffer, int* len, int* K) {
61-
static const uint32_t kPow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
61+
static const uint64_t kPow10[] = { 1U, 10U, 100U, 1000U, 10000U, 100000U, 1000000U, 10000000U, 100000000U,
62+
1000000000U, 10000000000U, 100000000000U, 1000000000000U,
63+
10000000000000U, 100000000000000U, 1000000000000000U,
64+
10000000000000000U, 100000000000000000U, 1000000000000000000U,
65+
10000000000000000000U };
6266
const DiyFp one(uint64_t(1) << -Mp.e, Mp.e);
6367
const DiyFp wp_w = Mp - W;
6468
uint32_t p1 = static_cast<uint32_t>(Mp.f >> -one.e);
@@ -86,7 +90,7 @@ inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buff
8690
uint64_t tmp = (static_cast<uint64_t>(p1) << -one.e) + p2;
8791
if (tmp <= delta) {
8892
*K += kappa;
89-
GrisuRound(buffer, *len, delta, tmp, static_cast<uint64_t>(kPow10[kappa]) << -one.e, wp_w.f);
93+
GrisuRound(buffer, *len, delta, tmp, kPow10[kappa] << -one.e, wp_w.f);
9094
return;
9195
}
9296
}
@@ -103,7 +107,7 @@ inline void DigitGen(const DiyFp& W, const DiyFp& Mp, uint64_t delta, char* buff
103107
if (p2 < delta) {
104108
*K += kappa;
105109
int index = -kappa;
106-
GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 9 ? kPow10[index] : 0));
110+
GrisuRound(buffer, *len, delta, p2, one.f, wp_w.f * (index < 20 ? kPow10[index] : 0));
107111
return;
108112
}
109113
}

test/unittest/dtoatest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ TEST(dtoa, normal) {
3838
TEST_DTOA(0.123456789012, "0.123456789012");
3939
TEST_DTOA(1234567.8, "1234567.8");
4040
TEST_DTOA(-79.39773355813419, "-79.39773355813419");
41+
TEST_DTOA(-36.973846435546875, "-36.973846435546875");
4142
TEST_DTOA(0.000001, "0.000001");
4243
TEST_DTOA(0.0000001, "1e-7");
4344
TEST_DTOA(1e30, "1e30");

0 commit comments

Comments
 (0)