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
Conversation
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 What to do if you already signed the CLAIndividual signers
Corporate signers
ℹ️ Googlers: Go here for more info. |
@googlebot I signed it!) |
CLAs look good, thanks! ℹ️ Googlers: Go here for more info. |
There was a problem hiding this 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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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()); | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Thanks. |
@floitsch Thanks for your advice |
double-conversion/double-to-string.h
Outdated
@@ -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 |
There was a problem hiding this comment.
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."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or something similar.
The comment is updated |
double-conversion/double-to-string.h
Outdated
@@ -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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// When min_exponent_width is used for exponential representations. | |
// The min_exponent_width is used for exponential representations. |
One last tiny nit left. |
Thanks. |
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