Skip to content

Commit

Permalink
Explain that ToShortest might not be the shortest. (#245)
Browse files Browse the repository at this point in the history
Fixes #242.
  • Loading branch information
floitsch authored Mar 1, 2025
1 parent 4aecc84 commit e2fd4e0
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions double-conversion/double-to-string.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,24 @@ class DoubleToStringConverter {
// kBase10MaximalLength significant digits).
// "-1.7976931348623157e+308", "-1.7976931348623157E308"
// In addition, the buffer must be able to hold the trailing '\0' character.
//
// Since the algorithm finds the shortest number of significant digits, it
// can produce an output that isn't the shortest possible if the
// decimal_in_shortest_high is high enough. For example, the number
// 1e23 could be written as 99999999999999991611392 with 23
// digits, however, it only needs one significant digit 1, and thus the
// result is 100000000000000000000000, which has 24 digits.
bool ToShortest(double value, StringBuilder* result_builder) const {
return ToShortestIeeeNumber(value, result_builder, SHORTEST);
}

// Same as ToShortest, but for single-precision floats.
//
// Since the algorithm finds the shortest number of significant digits, it
// can, in very rare cases, produce an output that isn't the shortest possible.
// For example, the number 1e11f could be written as 99999997952 with 11
// digits, however, it only needs one significant digit 1, and thus the
// result is 100000000000, which has 12 digits.
bool ToShortestSingle(float value, StringBuilder* result_builder) const {
return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE);
}
Expand Down

0 comments on commit e2fd4e0

Please sign in to comment.