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