double-conversion-double-to-string.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // © 2018 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. //
  4. // From the double-conversion library. Original license:
  5. //
  6. // Copyright 2012 the V8 project authors. All rights reserved.
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. //
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following
  15. // disclaimer in the documentation and/or other materials provided
  16. // with the distribution.
  17. // * Neither the name of Google Inc. nor the names of its
  18. // contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. // ICU PATCH: ifdef around UCONFIG_NO_FORMATTING
  33. #include "unicode/utypes.h"
  34. #if !UCONFIG_NO_FORMATTING
  35. #ifndef DOUBLE_CONVERSION_DOUBLE_TO_STRING_H_
  36. #define DOUBLE_CONVERSION_DOUBLE_TO_STRING_H_
  37. // ICU PATCH: Customize header file paths for ICU.
  38. #include "double-conversion-utils.h"
  39. // ICU PATCH: Wrap in ICU namespace
  40. U_NAMESPACE_BEGIN
  41. namespace double_conversion {
  42. class DoubleToStringConverter {
  43. public:
  44. #if 0 // not needed for ICU
  45. // When calling ToFixed with a double > 10^kMaxFixedDigitsBeforePoint
  46. // or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the
  47. // function returns false.
  48. static const int kMaxFixedDigitsBeforePoint = 60;
  49. static const int kMaxFixedDigitsAfterPoint = 60;
  50. // When calling ToExponential with a requested_digits
  51. // parameter > kMaxExponentialDigits then the function returns false.
  52. static const int kMaxExponentialDigits = 120;
  53. // When calling ToPrecision with a requested_digits
  54. // parameter < kMinPrecisionDigits or requested_digits > kMaxPrecisionDigits
  55. // then the function returns false.
  56. static const int kMinPrecisionDigits = 1;
  57. static const int kMaxPrecisionDigits = 120;
  58. enum Flags {
  59. NO_FLAGS = 0,
  60. EMIT_POSITIVE_EXPONENT_SIGN = 1,
  61. EMIT_TRAILING_DECIMAL_POINT = 2,
  62. EMIT_TRAILING_ZERO_AFTER_POINT = 4,
  63. UNIQUE_ZERO = 8
  64. };
  65. // Flags should be a bit-or combination of the possible Flags-enum.
  66. // - NO_FLAGS: no special flags.
  67. // - EMIT_POSITIVE_EXPONENT_SIGN: when the number is converted into exponent
  68. // form, emits a '+' for positive exponents. Example: 1.2e+2.
  69. // - EMIT_TRAILING_DECIMAL_POINT: when the input number is an integer and is
  70. // converted into decimal format then a trailing decimal point is appended.
  71. // Example: 2345.0 is converted to "2345.".
  72. // - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point
  73. // emits a trailing '0'-character. This flag requires the
  74. // EXMIT_TRAILING_DECIMAL_POINT flag.
  75. // Example: 2345.0 is converted to "2345.0".
  76. // - UNIQUE_ZERO: "-0.0" is converted to "0.0".
  77. //
  78. // Infinity symbol and nan_symbol provide the string representation for these
  79. // special values. If the string is NULL and the special value is encountered
  80. // then the conversion functions return false.
  81. //
  82. // The exponent_character is used in exponential representations. It is
  83. // usually 'e' or 'E'.
  84. //
  85. // When converting to the shortest representation the converter will
  86. // represent input numbers in decimal format if they are in the interval
  87. // [10^decimal_in_shortest_low; 10^decimal_in_shortest_high[
  88. // (lower boundary included, greater boundary excluded).
  89. // Example: with decimal_in_shortest_low = -6 and
  90. // decimal_in_shortest_high = 21:
  91. // ToShortest(0.000001) -> "0.000001"
  92. // ToShortest(0.0000001) -> "1e-7"
  93. // ToShortest(111111111111111111111.0) -> "111111111111111110000"
  94. // ToShortest(100000000000000000000.0) -> "100000000000000000000"
  95. // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21"
  96. //
  97. // When converting to precision mode the converter may add
  98. // max_leading_padding_zeroes before returning the number in exponential
  99. // format.
  100. // Example with max_leading_padding_zeroes_in_precision_mode = 6.
  101. // ToPrecision(0.0000012345, 2) -> "0.0000012"
  102. // ToPrecision(0.00000012345, 2) -> "1.2e-7"
  103. // Similarily the converter may add up to
  104. // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid
  105. // returning an exponential representation. A zero added by the
  106. // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit.
  107. // Examples for max_trailing_padding_zeroes_in_precision_mode = 1:
  108. // ToPrecision(230.0, 2) -> "230"
  109. // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT.
  110. // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
  111. //
  112. // The min_exponent_width is used for exponential representations.
  113. // The converter adds leading '0's to the exponent until the exponent
  114. // is at least min_exponent_width digits long.
  115. // The min_exponent_width is clamped to 5.
  116. // As such, the exponent may never have more than 5 digits in total.
  117. DoubleToStringConverter(int flags,
  118. const char* infinity_symbol,
  119. const char* nan_symbol,
  120. char exponent_character,
  121. int decimal_in_shortest_low,
  122. int decimal_in_shortest_high,
  123. int max_leading_padding_zeroes_in_precision_mode,
  124. int max_trailing_padding_zeroes_in_precision_mode,
  125. int min_exponent_width = 0)
  126. : flags_(flags),
  127. infinity_symbol_(infinity_symbol),
  128. nan_symbol_(nan_symbol),
  129. exponent_character_(exponent_character),
  130. decimal_in_shortest_low_(decimal_in_shortest_low),
  131. decimal_in_shortest_high_(decimal_in_shortest_high),
  132. max_leading_padding_zeroes_in_precision_mode_(
  133. max_leading_padding_zeroes_in_precision_mode),
  134. max_trailing_padding_zeroes_in_precision_mode_(
  135. max_trailing_padding_zeroes_in_precision_mode),
  136. min_exponent_width_(min_exponent_width) {
  137. // When 'trailing zero after the point' is set, then 'trailing point'
  138. // must be set too.
  139. DOUBLE_CONVERSION_ASSERT(((flags & EMIT_TRAILING_DECIMAL_POINT) != 0) ||
  140. !((flags & EMIT_TRAILING_ZERO_AFTER_POINT) != 0));
  141. }
  142. // Returns a converter following the EcmaScript specification.
  143. static const DoubleToStringConverter& EcmaScriptConverter();
  144. // Computes the shortest string of digits that correctly represent the input
  145. // number. Depending on decimal_in_shortest_low and decimal_in_shortest_high
  146. // (see constructor) it then either returns a decimal representation, or an
  147. // exponential representation.
  148. // Example with decimal_in_shortest_low = -6,
  149. // decimal_in_shortest_high = 21,
  150. // EMIT_POSITIVE_EXPONENT_SIGN activated, and
  151. // EMIT_TRAILING_DECIMAL_POINT deactived:
  152. // ToShortest(0.000001) -> "0.000001"
  153. // ToShortest(0.0000001) -> "1e-7"
  154. // ToShortest(111111111111111111111.0) -> "111111111111111110000"
  155. // ToShortest(100000000000000000000.0) -> "100000000000000000000"
  156. // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21"
  157. //
  158. // Note: the conversion may round the output if the returned string
  159. // is accurate enough to uniquely identify the input-number.
  160. // For example the most precise representation of the double 9e59 equals
  161. // "899999999999999918767229449717619953810131273674690656206848", but
  162. // the converter will return the shorter (but still correct) "9e59".
  163. //
  164. // Returns true if the conversion succeeds. The conversion always succeeds
  165. // except when the input value is special and no infinity_symbol or
  166. // nan_symbol has been given to the constructor.
  167. bool ToShortest(double value, StringBuilder* result_builder) const {
  168. return ToShortestIeeeNumber(value, result_builder, SHORTEST);
  169. }
  170. // Same as ToShortest, but for single-precision floats.
  171. bool ToShortestSingle(float value, StringBuilder* result_builder) const {
  172. return ToShortestIeeeNumber(value, result_builder, SHORTEST_SINGLE);
  173. }
  174. // Computes a decimal representation with a fixed number of digits after the
  175. // decimal point. The last emitted digit is rounded.
  176. //
  177. // Examples:
  178. // ToFixed(3.12, 1) -> "3.1"
  179. // ToFixed(3.1415, 3) -> "3.142"
  180. // ToFixed(1234.56789, 4) -> "1234.5679"
  181. // ToFixed(1.23, 5) -> "1.23000"
  182. // ToFixed(0.1, 4) -> "0.1000"
  183. // ToFixed(1e30, 2) -> "1000000000000000019884624838656.00"
  184. // ToFixed(0.1, 30) -> "0.100000000000000005551115123126"
  185. // ToFixed(0.1, 17) -> "0.10000000000000001"
  186. //
  187. // If requested_digits equals 0, then the tail of the result depends on
  188. // the EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT.
  189. // Examples, for requested_digits == 0,
  190. // let EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT be
  191. // - false and false: then 123.45 -> 123
  192. // 0.678 -> 1
  193. // - true and false: then 123.45 -> 123.
  194. // 0.678 -> 1.
  195. // - true and true: then 123.45 -> 123.0
  196. // 0.678 -> 1.0
  197. //
  198. // Returns true if the conversion succeeds. The conversion always succeeds
  199. // except for the following cases:
  200. // - the input value is special and no infinity_symbol or nan_symbol has
  201. // been provided to the constructor,
  202. // - 'value' > 10^kMaxFixedDigitsBeforePoint, or
  203. // - 'requested_digits' > kMaxFixedDigitsAfterPoint.
  204. // The last two conditions imply that the result will never contain more than
  205. // 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters
  206. // (one additional character for the sign, and one for the decimal point).
  207. bool ToFixed(double value,
  208. int requested_digits,
  209. StringBuilder* result_builder) const;
  210. // Computes a representation in exponential format with requested_digits
  211. // after the decimal point. The last emitted digit is rounded.
  212. // If requested_digits equals -1, then the shortest exponential representation
  213. // is computed.
  214. //
  215. // Examples with EMIT_POSITIVE_EXPONENT_SIGN deactivated, and
  216. // exponent_character set to 'e'.
  217. // ToExponential(3.12, 1) -> "3.1e0"
  218. // ToExponential(5.0, 3) -> "5.000e0"
  219. // ToExponential(0.001, 2) -> "1.00e-3"
  220. // ToExponential(3.1415, -1) -> "3.1415e0"
  221. // ToExponential(3.1415, 4) -> "3.1415e0"
  222. // ToExponential(3.1415, 3) -> "3.142e0"
  223. // ToExponential(123456789000000, 3) -> "1.235e14"
  224. // ToExponential(1000000000000000019884624838656.0, -1) -> "1e30"
  225. // ToExponential(1000000000000000019884624838656.0, 32) ->
  226. // "1.00000000000000001988462483865600e30"
  227. // ToExponential(1234, 0) -> "1e3"
  228. //
  229. // Returns true if the conversion succeeds. The conversion always succeeds
  230. // except for the following cases:
  231. // - the input value is special and no infinity_symbol or nan_symbol has
  232. // been provided to the constructor,
  233. // - 'requested_digits' > kMaxExponentialDigits.
  234. // The last condition implies that the result will never contain more than
  235. // kMaxExponentialDigits + 8 characters (the sign, the digit before the
  236. // decimal point, the decimal point, the exponent character, the
  237. // exponent's sign, and at most 3 exponent digits).
  238. bool ToExponential(double value,
  239. int requested_digits,
  240. StringBuilder* result_builder) const;
  241. // Computes 'precision' leading digits of the given 'value' and returns them
  242. // either in exponential or decimal format, depending on
  243. // max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the
  244. // constructor).
  245. // The last computed digit is rounded.
  246. //
  247. // Example with max_leading_padding_zeroes_in_precision_mode = 6.
  248. // ToPrecision(0.0000012345, 2) -> "0.0000012"
  249. // ToPrecision(0.00000012345, 2) -> "1.2e-7"
  250. // Similarily the converter may add up to
  251. // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid
  252. // returning an exponential representation. A zero added by the
  253. // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit.
  254. // Examples for max_trailing_padding_zeroes_in_precision_mode = 1:
  255. // ToPrecision(230.0, 2) -> "230"
  256. // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT.
  257. // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
  258. // Examples for max_trailing_padding_zeroes_in_precision_mode = 3, and no
  259. // EMIT_TRAILING_ZERO_AFTER_POINT:
  260. // ToPrecision(123450.0, 6) -> "123450"
  261. // ToPrecision(123450.0, 5) -> "123450"
  262. // ToPrecision(123450.0, 4) -> "123500"
  263. // ToPrecision(123450.0, 3) -> "123000"
  264. // ToPrecision(123450.0, 2) -> "1.2e5"
  265. //
  266. // Returns true if the conversion succeeds. The conversion always succeeds
  267. // except for the following cases:
  268. // - the input value is special and no infinity_symbol or nan_symbol has
  269. // been provided to the constructor,
  270. // - precision < kMinPericisionDigits
  271. // - precision > kMaxPrecisionDigits
  272. // The last condition implies that the result will never contain more than
  273. // kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the
  274. // exponent character, the exponent's sign, and at most 3 exponent digits).
  275. bool ToPrecision(double value,
  276. int precision,
  277. StringBuilder* result_builder) const;
  278. #endif // not needed for ICU
  279. enum DtoaMode {
  280. // Produce the shortest correct representation.
  281. // For example the output of 0.299999999999999988897 is (the less accurate
  282. // but correct) 0.3.
  283. SHORTEST,
  284. // Same as SHORTEST, but for single-precision floats.
  285. SHORTEST_SINGLE,
  286. // Produce a fixed number of digits after the decimal point.
  287. // For instance fixed(0.1, 4) becomes 0.1000
  288. // If the input number is big, the output will be big.
  289. FIXED,
  290. // Fixed number of digits (independent of the decimal point).
  291. PRECISION
  292. };
  293. // The maximal number of digits that are needed to emit a double in base 10.
  294. // A higher precision can be achieved by using more digits, but the shortest
  295. // accurate representation of any double will never use more digits than
  296. // kBase10MaximalLength.
  297. // Note that DoubleToAscii null-terminates its input. So the given buffer
  298. // should be at least kBase10MaximalLength + 1 characters long.
  299. static const int kBase10MaximalLength = 17;
  300. // Converts the given double 'v' to digit characters. 'v' must not be NaN,
  301. // +Infinity, or -Infinity. In SHORTEST_SINGLE-mode this restriction also
  302. // applies to 'v' after it has been casted to a single-precision float. That
  303. // is, in this mode static_cast<float>(v) must not be NaN, +Infinity or
  304. // -Infinity.
  305. //
  306. // The result should be interpreted as buffer * 10^(point-length).
  307. //
  308. // The digits are written to the buffer in the platform's charset, which is
  309. // often UTF-8 (with ASCII-range digits) but may be another charset, such
  310. // as EBCDIC.
  311. //
  312. // The output depends on the given mode:
  313. // - SHORTEST: produce the least amount of digits for which the internal
  314. // identity requirement is still satisfied. If the digits are printed
  315. // (together with the correct exponent) then reading this number will give
  316. // 'v' again. The buffer will choose the representation that is closest to
  317. // 'v'. If there are two at the same distance, than the one farther away
  318. // from 0 is chosen (halfway cases - ending with 5 - are rounded up).
  319. // In this mode the 'requested_digits' parameter is ignored.
  320. // - SHORTEST_SINGLE: same as SHORTEST but with single-precision.
  321. // - FIXED: produces digits necessary to print a given number with
  322. // 'requested_digits' digits after the decimal point. The produced digits
  323. // might be too short in which case the caller has to fill the remainder
  324. // with '0's.
  325. // Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2.
  326. // Halfway cases are rounded towards +/-Infinity (away from 0). The call
  327. // toFixed(0.15, 2) thus returns buffer="2", point=0.
  328. // The returned buffer may contain digits that would be truncated from the
  329. // shortest representation of the input.
  330. // - PRECISION: produces 'requested_digits' where the first digit is not '0'.
  331. // Even though the length of produced digits usually equals
  332. // 'requested_digits', the function is allowed to return fewer digits, in
  333. // which case the caller has to fill the missing digits with '0's.
  334. // Halfway cases are again rounded away from 0.
  335. // DoubleToAscii expects the given buffer to be big enough to hold all
  336. // digits and a terminating null-character. In SHORTEST-mode it expects a
  337. // buffer of at least kBase10MaximalLength + 1. In all other modes the
  338. // requested_digits parameter and the padding-zeroes limit the size of the
  339. // output. Don't forget the decimal point, the exponent character and the
  340. // terminating null-character when computing the maximal output size.
  341. // The given length is only used in debug mode to ensure the buffer is big
  342. // enough.
  343. // ICU PATCH: Export this as U_I18N_API for unit tests.
  344. static void U_I18N_API DoubleToAscii(double v,
  345. DtoaMode mode,
  346. int requested_digits,
  347. char* buffer,
  348. int buffer_length,
  349. bool* sign,
  350. int* length,
  351. int* point);
  352. #if 0 // not needed for ICU
  353. private:
  354. // Implementation for ToShortest and ToShortestSingle.
  355. bool ToShortestIeeeNumber(double value,
  356. StringBuilder* result_builder,
  357. DtoaMode mode) const;
  358. // If the value is a special value (NaN or Infinity) constructs the
  359. // corresponding string using the configured infinity/nan-symbol.
  360. // If either of them is NULL or the value is not special then the
  361. // function returns false.
  362. bool HandleSpecialValues(double value, StringBuilder* result_builder) const;
  363. // Constructs an exponential representation (i.e. 1.234e56).
  364. // The given exponent assumes a decimal point after the first decimal digit.
  365. void CreateExponentialRepresentation(const char* decimal_digits,
  366. int length,
  367. int exponent,
  368. StringBuilder* result_builder) const;
  369. // Creates a decimal representation (i.e 1234.5678).
  370. void CreateDecimalRepresentation(const char* decimal_digits,
  371. int length,
  372. int decimal_point,
  373. int digits_after_point,
  374. StringBuilder* result_builder) const;
  375. const int flags_;
  376. const char* const infinity_symbol_;
  377. const char* const nan_symbol_;
  378. const char exponent_character_;
  379. const int decimal_in_shortest_low_;
  380. const int decimal_in_shortest_high_;
  381. const int max_leading_padding_zeroes_in_precision_mode_;
  382. const int max_trailing_padding_zeroes_in_precision_mode_;
  383. const int min_exponent_width_;
  384. #endif // not needed for ICU
  385. DOUBLE_CONVERSION_DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter);
  386. };
  387. } // namespace double_conversion
  388. // ICU PATCH: Close ICU namespace
  389. U_NAMESPACE_END
  390. #endif // DOUBLE_CONVERSION_DOUBLE_TO_STRING_H_
  391. #endif // ICU PATCH: close #if !UCONFIG_NO_FORMATTING