Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add min exponent width option in double-to-string shortest conversion #116

Merged
merged 1 commit into from Aug 1, 2019

Conversation

pengzhenyi2015
Copy link
Contributor

Prefix 0 is added when call golang fmt.Sprint() to convert double to shortest string
For example: 0.00001 is convert to 1e-05, but not 1e-5

In some situations, I want to convert double to shortest string in "golang format", so a "min exponent width option" will be useful

@googlebot
Copy link
Collaborator

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it!) and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@pengzhenyi2015
Copy link
Contributor Author

@googlebot I signed it!)

@googlebot
Copy link
Collaborator

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

Copy link
Collaborator

@floitsch floitsch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also update the Changelog.
Feel free to add yourself to the AUTHORS file as well.

@@ -111,7 +111,8 @@ class DoubleToStringConverter {
int decimal_in_shortest_low,
int decimal_in_shortest_high,
int max_leading_padding_zeroes_in_precision_mode,
int max_trailing_padding_zeroes_in_precision_mode)
int max_trailing_padding_zeroes_in_precision_mode,
int min_exponent_width = 0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add a line (or two) to the comment of the constructor, explaining what the parameter does.
Shouldn't need a lot of explanation (quite self-explanatory), but mention that the exponent will never have more than 5 digits.
Then also add a comment to the constant (5) in double-to-string.cc and state that changing this constant also requires updating the comment in double-to-string.h

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your advice, I added some comments to introduce this option

builder.Reset();
CHECK(dcExpWidth.ToShortest(1000000000.0, &builder));
CHECK_EQ("1e+09", builder.Finalize());

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a few more tests with corner-cases:

  • min_exponent_width set to 0. (which is the default, but still)
  • min_exponent_width set to 100.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The corner-cases are added

@floitsch
Copy link
Collaborator

floitsch commented Aug 1, 2019

Thanks.

@pengzhenyi2015
Copy link
Contributor Author

@floitsch Thanks for your advice

@@ -104,14 +104,18 @@ class DoubleToStringConverter {
// ToPrecision(230.0, 2) -> "230"
// ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT.
// ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
//
// The exponent width may add up to min_exponent_width by adding prefix '0',
// but the exponent will never have more than 5 digits as kMaxExponentLength is 5
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to mention kMaxExponentLength. It's an internal detail.
Simply:
"When min_exponent_width is used for exponential representations. The converter adds leading '0's, to the exponent until the exponent is at least min_exponent_width digits long.
The min_exponent_width is clamped to 5. As such, the exponent may never have more than 5 digits in total."

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or something similar.

@pengzhenyi2015
Copy link
Contributor Author

The comment is updated

@@ -104,14 +104,21 @@ class DoubleToStringConverter {
// ToPrecision(230.0, 2) -> "230"
// ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT.
// ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
//
// When min_exponent_width is used for exponential representations.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// When min_exponent_width is used for exponential representations.
// The min_exponent_width is used for exponential representations.

@floitsch
Copy link
Collaborator

floitsch commented Aug 1, 2019

One last tiny nit left.

@pengzhenyi2015 pengzhenyi2015 reopened this Aug 1, 2019
@floitsch floitsch merged commit 7da5db9 into google:master Aug 1, 2019
@floitsch
Copy link
Collaborator

floitsch commented Aug 1, 2019

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants