Skip to content

Commit

Permalink
Allow 100 decimal digits for 'toFixed'. (#156)
Browse files Browse the repository at this point in the history
Fixes #152.
  • Loading branch information
floitsch authored Feb 20, 2021
1 parent 0ee0fcd commit 95c7131
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
2 changes: 1 addition & 1 deletion double-conversion/double-to-string.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class DoubleToStringConverter {
// or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the
// function returns false.
static const int kMaxFixedDigitsBeforePoint = 60;
static const int kMaxFixedDigitsAfterPoint = 60;
static const int kMaxFixedDigitsAfterPoint = 100;

// When calling ToExponential with a requested_digits
// parameter > kMaxExponentialDigits then the function returns false.
Expand Down
87 changes: 82 additions & 5 deletions test/cctest/test-conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ TEST(DoubleToShortestSingle) {


TEST(DoubleToFixed) {
const int kBufferSize = 128;
const int kBufferSize = 168;
char buffer[kBufferSize];
StringBuilder builder(buffer, kBufferSize);
int flags = DoubleToStringConverter::EMIT_POSITIVE_EXPONENT_SIGN |
Expand All @@ -462,25 +462,98 @@ TEST(DoubleToFixed) {
CHECK_EQ("0.0", builder.Finalize());

DOUBLE_CONVERSION_ASSERT(DoubleToStringConverter::kMaxFixedDigitsBeforePoint == 60);
DOUBLE_CONVERSION_ASSERT(DoubleToStringConverter::kMaxFixedDigitsAfterPoint == 60);
DOUBLE_CONVERSION_ASSERT(DoubleToStringConverter::kMaxFixedDigitsAfterPoint == 100);

// Most of the 100 digit tests were copied from
// https://searchfox.org/mozilla-central/source/js/src/tests/non262/Number/toFixed-values.js.

builder.Reset();
CHECK(dc.ToFixed(
0.0, DoubleToStringConverter::kMaxFixedDigitsAfterPoint, &builder));
CHECK_EQ("0.000000000000000000000000000000000000000000000000000000000000",
CHECK_EQ("0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
builder.Finalize());

builder.Reset();
CHECK(dc.ToFixed(
9e59, DoubleToStringConverter::kMaxFixedDigitsAfterPoint, &builder));
CHECK_EQ("899999999999999918767229449717619953810131273674690656206848."
"000000000000000000000000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
builder.Finalize());

builder.Reset();
CHECK(dc.ToFixed(
-9e59, DoubleToStringConverter::kMaxFixedDigitsAfterPoint, &builder));
CHECK_EQ("-899999999999999918767229449717619953810131273674690656206848."
"000000000000000000000000000000000000000000000000000000000000",
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
builder.Finalize());

builder.Reset();
CHECK(dc.ToFixed(
1e-100, DoubleToStringConverter::kMaxFixedDigitsAfterPoint, &builder));
CHECK_EQ("0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
builder.Finalize());

builder.Reset();
CHECK(dc.ToFixed(0.3000000000000000444089209850062616169452667236328125,
DoubleToStringConverter::kMaxFixedDigitsAfterPoint,
&builder));
CHECK_EQ("0.3000000000000000444089209850062616169452667236328125000000000000000000000000000000000000000000000000",
builder.Finalize());

builder.Reset();
CHECK(dc.ToFixed(1.5e-100,
DoubleToStringConverter::kMaxFixedDigitsAfterPoint,
&builder));
CHECK_EQ("0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002",
builder.Finalize());

builder.Reset();
CHECK(dc.ToFixed(1.15e-99, // In reality: 1.14999999999999992147301128036734...
DoubleToStringConverter::kMaxFixedDigitsAfterPoint,
&builder));
CHECK_EQ("0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011",
builder.Finalize());

builder.Reset();
CHECK(dc.ToFixed(3.141592653589793,
DoubleToStringConverter::kMaxFixedDigitsAfterPoint,
&builder));
CHECK_EQ("3.1415926535897931159979634685441851615905761718750000000000000000000000000000000000000000000000000000",
builder.Finalize());

builder.Reset();
CHECK(dc.ToFixed(1.0,
DoubleToStringConverter::kMaxFixedDigitsAfterPoint,
&builder));
CHECK_EQ("1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
builder.Finalize());

builder.Reset();
CHECK(dc.ToFixed(-123456.78,
DoubleToStringConverter::kMaxFixedDigitsAfterPoint,
&builder));
CHECK_EQ("-123456.7799999999988358467817306518554687500000000000000000000000000000000000000000000000000000000000000000",
builder.Finalize());

builder.Reset();
CHECK(dc.ToFixed(123456.78,
DoubleToStringConverter::kMaxFixedDigitsAfterPoint,
&builder));
CHECK_EQ("123456.7799999999988358467817306518554687500000000000000000000000000000000000000000000000000000000000000000",
builder.Finalize());

builder.Reset();
CHECK(dc.ToFixed(100000000000000000000.0,
DoubleToStringConverter::kMaxFixedDigitsAfterPoint,
&builder));
CHECK_EQ("100000000000000000000.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
builder.Finalize());

builder.Reset();
CHECK(dc.ToFixed(-100000000000000000000.0,
DoubleToStringConverter::kMaxFixedDigitsAfterPoint,
&builder));
CHECK_EQ("-100000000000000000000.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
builder.Finalize());

builder.Reset();
Expand Down Expand Up @@ -637,6 +710,10 @@ TEST(DoubleToFixed) {
CHECK(dc5.ToFixed(0.1, 30, &builder));
CHECK_EQ("0.100000000000000005551115123126", builder.Finalize());

builder.Reset();
CHECK(dc5.ToFixed(0.1, 100, &builder));
CHECK_EQ("0.1000000000000000055511151231257827021181583404541015625000000000000000000000000000000000000000000000", builder.Finalize());

builder.Reset();
CHECK(dc5.ToFixed(0.1, 17, &builder));
CHECK_EQ("0.10000000000000001", builder.Finalize());
Expand Down

0 comments on commit 95c7131

Please sign in to comment.