During StringToDouble negative exponents may be less than -999 with a result that is not 0.0.

Review URL: http://codereview.chromium.org/3564011

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@5594 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
floitschV8@gmail.com 2010-10-06 08:33:52 +00:00
parent 55356a3627
commit 9c00ea744b
2 changed files with 40 additions and 2 deletions

View File

@ -665,9 +665,15 @@ static double InternalStringToDouble(Iterator current,
buffer[buffer_pos++] = '-'; buffer[buffer_pos++] = '-';
exponent = -exponent; exponent = -exponent;
} }
if (exponent > 999) exponent = 999; // Result will be Infinity or 0 or -0.
const int exp_digits = 3; // The minimal/maximal double is +/-1.7e-308. Given that
// the buffer contains at most 773 (kMaxSignificantDigits + 1) the
// minimal possible exponent is hence -(308 + 773)=-1081.
// Since leading zeros are removed the maximal exponent cannot exceed 308.
// If the following test triggers the result will be +/-infinity or +/-0.
if (exponent > 9999) exponent = 9999;
const int exp_digits = 4;
for (int i = 0; i < exp_digits; i++) { for (int i = 0; i < exp_digits; i++) {
buffer[buffer_pos + exp_digits - 1 - i] = '0' + exponent % 10; buffer[buffer_pos + exp_digits - 1 - i] = '0' + exponent % 10;
exponent /= 10; exponent /= 10;

View File

@ -168,6 +168,38 @@ TEST(MaximumSignificantDigits) {
CHECK_EQ(gay_strtod(num, NULL), StringToDouble(num, NO_FLAGS)); CHECK_EQ(gay_strtod(num, NULL), StringToDouble(num, NO_FLAGS));
} }
TEST(MinimumExponent) {
// Same test but with different point-position.
char num[] =
"445014771701440202508199667279499186358524265859260511351695091"
"228726223124931264069530541271189424317838013700808305231545782"
"515453032382772695923684574304409936197089118747150815050941806"
"048037511737832041185193533879641611520514874130831632725201246"
"060231058690536206311752656217652146466431814205051640436322226"
"680064743260560117135282915796422274554896821334728738317548403"
"413978098469341510556195293821919814730032341053661708792231510"
"873354131880491105553390278848567812190177545006298062245710295"
"816371174594568773301103242116891776567137054973871082078224775"
"842509670618916870627821633352993761380751142008862499795052791"
"018709663463944015644907297315659352441231715398102212132212018"
"470035807616260163568645811358486831521563686919762403704226016"
"998291015625000000000000000000000000000000000e-1108";
CHECK_EQ(gay_strtod(num, NULL), StringToDouble(num, NO_FLAGS));
// Changes the result of strtod (at least in glibc implementation).
num[sizeof(num) - 8] = '1';
CHECK_EQ(gay_strtod(num, NULL), StringToDouble(num, NO_FLAGS));
}
TEST(MaximumExponent) {
char num[] = "0.16e309";
CHECK_EQ(gay_strtod(num, NULL), StringToDouble(num, NO_FLAGS));
}
TEST(ExponentNumberStr) { TEST(ExponentNumberStr) {
CHECK_EQ(1e1, StringToDouble("1e1", NO_FLAGS)); CHECK_EQ(1e1, StringToDouble("1e1", NO_FLAGS));