From 4824f12efbf01af72b8cb6fc96fae7b097b73015 Mon Sep 17 00:00:00 2001 From: miloyip Date: Tue, 14 Apr 2015 13:59:05 +0800 Subject: [PATCH] Fixed a bug in trimming long number sequence --- include/rapidjson/internal/strtod.h | 4 +++- test/unittest/readertest.cpp | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/internal/strtod.h b/include/rapidjson/internal/strtod.h index 805c752..980b2c5 100644 --- a/include/rapidjson/internal/strtod.h +++ b/include/rapidjson/internal/strtod.h @@ -258,7 +258,9 @@ inline double StrtodFullPrecision(double d, int p, const char* decimals, size_t // Trim right-most digits const int kMaxDecimalDigit = 780; if ((int)length > kMaxDecimalDigit) { - exp += (int(length) - kMaxDecimalDigit); + int delta = (int(length) - kMaxDecimalDigit); + exp += delta; + decimalPosition -= delta; length = kMaxDecimalDigit; } diff --git a/test/unittest/readertest.cpp b/test/unittest/readertest.cpp index 6115609..bccfbd6 100644 --- a/test/unittest/readertest.cpp +++ b/test/unittest/readertest.cpp @@ -289,6 +289,19 @@ static void TestParseDouble() { TEST_DOUBLE(fullPrecision, n1e308, 1E308); } + // Cover trimming + TEST_DOUBLE(fullPrecision, +"2.22507385850720113605740979670913197593481954635164564802342610972482222202107694551652952390813508" +"7914149158913039621106870086438694594645527657207407820621743379988141063267329253552286881372149012" +"9811224514518898490572223072852551331557550159143974763979834118019993239625482890171070818506906306" +"6665599493827577257201576306269066333264756530000924588831643303777979186961204949739037782970490505" +"1080609940730262937128958950003583799967207254304360284078895771796150945516748243471030702609144621" +"5722898802581825451803257070188608721131280795122334262883686223215037756666225039825343359745688844" +"2390026549819838548794829220689472168983109969836584681402285424333066033985088644580400103493397042" +"7567186443383770486037861622771738545623065874679014086723327636718751234567890123456789012345678901" +"e-308", + 2.2250738585072014e-308); + #if 0 // Very slow static const unsigned count = 10000000; // Random test for double