Merge pull request #34 from miloyip/SmallFloatingPoint

Fixes parsing small floating point values underflow
This commit is contained in:
Milo Yip 2014-06-30 21:46:34 +08:00
commit 1acfc21684
2 changed files with 11 additions and 3 deletions

View File

@ -664,7 +664,15 @@ private:
// Finish parsing, call event according to the type of number. // Finish parsing, call event according to the type of number.
if (useDouble) { if (useDouble) {
d *= internal::Pow10(exp + expFrac); int expSum = exp + expFrac;
if (expSum < -308) {
// Prevent expSum < -308, making Pow10(expSum) = 0
d *= internal::Pow10(exp);
d *= internal::Pow10(expFrac);
}
else
d *= internal::Pow10(expSum);
handler.Double(minus ? -d : d); handler.Double(minus ? -d : d);
} }
else { else {

View File

@ -129,9 +129,9 @@ TEST(Reader, ParseNumberHandler) {
TEST_DOUBLE("1.234E+10", 1.234E+10); TEST_DOUBLE("1.234E+10", 1.234E+10);
TEST_DOUBLE("1.234E-10", 1.234E-10); TEST_DOUBLE("1.234E-10", 1.234E-10);
TEST_DOUBLE("1.79769e+308", 1.79769e+308); TEST_DOUBLE("1.79769e+308", 1.79769e+308);
//TEST_DOUBLE("2.22507e-308", 2.22507e-308); // TODO: underflow TEST_DOUBLE("2.22507e-308", 2.22507e-308);
TEST_DOUBLE("-1.79769e+308", -1.79769e+308); TEST_DOUBLE("-1.79769e+308", -1.79769e+308);
//TEST_DOUBLE("-2.22507e-308", -2.22507e-308); // TODO: underflow TEST_DOUBLE("-2.22507e-308", -2.22507e-308);
TEST_DOUBLE("18446744073709551616", 18446744073709551616.0); // 2^64 (max of uint64_t + 1, force to use double) TEST_DOUBLE("18446744073709551616", 18446744073709551616.0); // 2^64 (max of uint64_t + 1, force to use double)
TEST_DOUBLE("-9223372036854775809", -9223372036854775809.0); // -2^63 - 1(min of int64_t + 1, force to use double) TEST_DOUBLE("-9223372036854775809", -9223372036854775809.0); // -2^63 - 1(min of int64_t + 1, force to use double)