Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: third_party/WebKit/Source/wtf/dtoa/double-conversion.h

Issue 1611343002: wtf reformat test Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: pydent Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 16 matching lines...) Expand all
27 27
28 #ifndef DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ 28 #ifndef DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_
29 #define DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ 29 #define DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_
30 30
31 #include "wtf/dtoa/utils.h" 31 #include "wtf/dtoa/utils.h"
32 32
33 namespace WTF { 33 namespace WTF {
34 34
35 namespace double_conversion { 35 namespace double_conversion {
36 36
37 class DoubleToStringConverter { 37 class DoubleToStringConverter {
38 public: 38 public:
39 // When calling ToFixed with a double > 10^kMaxFixedDigitsBeforePoint 39 // When calling ToFixed with a double > 10^kMaxFixedDigitsBeforePoint
40 // or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the 40 // or a requested_digits parameter > kMaxFixedDigitsAfterPoint then the
41 // function returns false. 41 // function returns false.
42 static const int kMaxFixedDigitsBeforePoint = 60; 42 static const int kMaxFixedDigitsBeforePoint = 60;
43 static const int kMaxFixedDigitsAfterPoint = 60; 43 static const int kMaxFixedDigitsAfterPoint = 60;
44 44
45 // When calling ToExponential with a requested_digits 45 // When calling ToExponential with a requested_digits
46 // parameter > kMaxExponentialDigits then the function returns false. 46 // parameter > kMaxExponentialDigits then the function returns false.
47 static const int kMaxExponentialDigits = 120; 47 static const int kMaxExponentialDigits = 120;
48 48
49 // When calling ToPrecision with a requested_digits 49 // When calling ToPrecision with a requested_digits
50 // parameter < kMinPrecisionDigits or requested_digits > kMaxPrecisionDi gits 50 // parameter < kMinPrecisionDigits or requested_digits > kMaxPrecisionDigits
51 // then the function returns false. 51 // then the function returns false.
52 static const int kMinPrecisionDigits = 1; 52 static const int kMinPrecisionDigits = 1;
53 static const int kMaxPrecisionDigits = 120; 53 static const int kMaxPrecisionDigits = 120;
54 54
55 enum Flags { 55 enum Flags {
56 NO_FLAGS = 0, 56 NO_FLAGS = 0,
57 EMIT_POSITIVE_EXPONENT_SIGN = 1, 57 EMIT_POSITIVE_EXPONENT_SIGN = 1,
58 EMIT_TRAILING_DECIMAL_POINT = 2, 58 EMIT_TRAILING_DECIMAL_POINT = 2,
59 EMIT_TRAILING_ZERO_AFTER_POINT = 4, 59 EMIT_TRAILING_ZERO_AFTER_POINT = 4,
60 UNIQUE_ZERO = 8 60 UNIQUE_ZERO = 8
61 }; 61 };
62 62
63 // Flags should be a bit-or combination of the possible Flags-enum. 63 // Flags should be a bit-or combination of the possible Flags-enum.
64 // - NO_FLAGS: no special flags. 64 // - NO_FLAGS: no special flags.
65 // - EMIT_POSITIVE_EXPONENT_SIGN: when the number is converted into exp onent 65 // - EMIT_POSITIVE_EXPONENT_SIGN: when the number is converted into exponent
66 // form, emits a '+' for positive exponents. Example: 1.2e+2. 66 // form, emits a '+' for positive exponents. Example: 1.2e+2.
67 // - EMIT_TRAILING_DECIMAL_POINT: when the input number is an integer a nd is 67 // - EMIT_TRAILING_DECIMAL_POINT: when the input number is an integer and is
68 // converted into decimal format then a trailing decimal point is app ended. 68 // converted into decimal format then a trailing decimal point is appended.
69 // Example: 2345.0 is converted to "2345.". 69 // Example: 2345.0 is converted to "2345.".
70 // - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point 70 // - EMIT_TRAILING_ZERO_AFTER_POINT: in addition to a trailing decimal point
71 // emits a trailing '0'-character. This flag requires the 71 // emits a trailing '0'-character. This flag requires the
72 // EXMIT_TRAILING_DECIMAL_POINT flag. 72 // EXMIT_TRAILING_DECIMAL_POINT flag.
73 // Example: 2345.0 is converted to "2345.0". 73 // Example: 2345.0 is converted to "2345.0".
74 // - UNIQUE_ZERO: "-0.0" is converted to "0.0". 74 // - UNIQUE_ZERO: "-0.0" is converted to "0.0".
75 // 75 //
76 // Infinity symbol and nan_symbol provide the string representation for these 76 // Infinity symbol and nan_symbol provide the string representation for these
77 // special values. If the string is NULL and the special value is encoun tered 77 // special values. If the string is NULL and the special value is encountered
78 // then the conversion functions return false. 78 // then the conversion functions return false.
79 // 79 //
80 // The exponent_character is used in exponential representations. It is 80 // The exponent_character is used in exponential representations. It is
81 // usually 'e' or 'E'. 81 // usually 'e' or 'E'.
82 // 82 //
83 // When converting to the shortest representation the converter will 83 // When converting to the shortest representation the converter will
84 // represent input numbers in decimal format if they are in the interval 84 // represent input numbers in decimal format if they are in the interval
85 // [10^decimal_in_shortest_low; 10^decimal_in_shortest_high[ 85 // [10^decimal_in_shortest_low; 10^decimal_in_shortest_high[
86 // (lower boundary included, greater boundary excluded). 86 // (lower boundary included, greater boundary excluded).
87 // Example: with decimal_in_shortest_low = -6 and 87 // Example: with decimal_in_shortest_low = -6 and
88 // decimal_in_shortest_high = 21: 88 // decimal_in_shortest_high = 21:
89 // ToShortest(0.000001) -> "0.000001" 89 // ToShortest(0.000001) -> "0.000001"
90 // ToShortest(0.0000001) -> "1e-7" 90 // ToShortest(0.0000001) -> "1e-7"
91 // ToShortest(111111111111111111111.0) -> "111111111111111110000" 91 // ToShortest(111111111111111111111.0) -> "111111111111111110000"
92 // ToShortest(100000000000000000000.0) -> "100000000000000000000" 92 // ToShortest(100000000000000000000.0) -> "100000000000000000000"
93 // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" 93 // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21"
94 // 94 //
95 // When converting to precision mode the converter may add 95 // When converting to precision mode the converter may add
96 // max_leading_padding_zeroes before returning the number in exponential 96 // max_leading_padding_zeroes before returning the number in exponential
97 // format. 97 // format.
98 // Example with max_leading_padding_zeroes_in_precision_mode = 6. 98 // Example with max_leading_padding_zeroes_in_precision_mode = 6.
99 // ToPrecision(0.0000012345, 2) -> "0.0000012" 99 // ToPrecision(0.0000012345, 2) -> "0.0000012"
100 // ToPrecision(0.00000012345, 2) -> "1.2e-7" 100 // ToPrecision(0.00000012345, 2) -> "1.2e-7"
101 // Similarily the converter may add up to 101 // Similarily the converter may add up to
102 // max_trailing_padding_zeroes_in_precision_mode in precision mode to av oid 102 // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid
103 // returning an exponential representation. A zero added by the 103 // returning an exponential representation. A zero added by the
104 // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. 104 // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit.
105 // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: 105 // Examples for max_trailing_padding_zeroes_in_precision_mode = 1:
106 // ToPrecision(230.0, 2) -> "230" 106 // ToPrecision(230.0, 2) -> "230"
107 // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. 107 // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT.
108 // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POIN T. 108 // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
109 DoubleToStringConverter(int flags, 109 DoubleToStringConverter(int flags,
110 const char* infinity_symbol, 110 const char* infinity_symbol,
111 const char* nan_symbol, 111 const char* nan_symbol,
112 char exponent_character, 112 char exponent_character,
113 int decimal_in_shortest_low, 113 int decimal_in_shortest_low,
114 int decimal_in_shortest_high, 114 int decimal_in_shortest_high,
115 int max_leading_padding_zeroes_in_precision_mode , 115 int max_leading_padding_zeroes_in_precision_mode,
116 int max_trailing_padding_zeroes_in_precision_mod e) 116 int max_trailing_padding_zeroes_in_precision_mode)
117 : flags_(flags), 117 : flags_(flags),
118 infinity_symbol_(infinity_symbol), 118 infinity_symbol_(infinity_symbol),
119 nan_symbol_(nan_symbol), 119 nan_symbol_(nan_symbol),
120 exponent_character_(exponent_character), 120 exponent_character_(exponent_character),
121 decimal_in_shortest_low_(decimal_in_shortest_low), 121 decimal_in_shortest_low_(decimal_in_shortest_low),
122 decimal_in_shortest_high_(decimal_in_shortest_high), 122 decimal_in_shortest_high_(decimal_in_shortest_high),
123 max_leading_padding_zeroes_in_precision_mode_( 123 max_leading_padding_zeroes_in_precision_mode_(
124 max_leading_padding_zeroes _in_precision_mode), 124 max_leading_padding_zeroes_in_precision_mode),
125 max_trailing_padding_zeroes_in_precision_mode_( 125 max_trailing_padding_zeroes_in_precision_mode_(
126 max_trailing_padding_zero es_in_precision_mode) { 126 max_trailing_padding_zeroes_in_precision_mode) {
127 // When 'trailing zero after the point' is set, then 'trailing point ' 127 // When 'trailing zero after the point' is set, then 'trailing point'
128 // must be set too. 128 // must be set too.
129 ASSERT(((flags & EMIT_TRAILING_DECIMAL_POINT) != 0) || 129 ASSERT(((flags & EMIT_TRAILING_DECIMAL_POINT) != 0) ||
130 !((flags & EMIT_TRAILING_ZERO_AFTER_POINT) != 0)); 130 !((flags & EMIT_TRAILING_ZERO_AFTER_POINT) != 0));
131 } 131 }
132 132
133 // Returns a converter following the EcmaScript specification. 133 // Returns a converter following the EcmaScript specification.
134 static const DoubleToStringConverter& EcmaScriptConverter(); 134 static const DoubleToStringConverter& EcmaScriptConverter();
135 135
136 // Computes the shortest string of digits that correctly represent the i nput 136 // Computes the shortest string of digits that correctly represent the input
137 // number. Depending on decimal_in_shortest_low and decimal_in_shortest_ high 137 // number. Depending on decimal_in_shortest_low and decimal_in_shortest_high
138 // (see constructor) it then either returns a decimal representation, or an 138 // (see constructor) it then either returns a decimal representation, or an
139 // exponential representation. 139 // exponential representation.
140 // Example with decimal_in_shortest_low = -6, 140 // Example with decimal_in_shortest_low = -6,
141 // decimal_in_shortest_high = 21, 141 // decimal_in_shortest_high = 21,
142 // EMIT_POSITIVE_EXPONENT_SIGN activated, and 142 // EMIT_POSITIVE_EXPONENT_SIGN activated, and
143 // EMIT_TRAILING_DECIMAL_POINT deactived: 143 // EMIT_TRAILING_DECIMAL_POINT deactived:
144 // ToShortest(0.000001) -> "0.000001" 144 // ToShortest(0.000001) -> "0.000001"
145 // ToShortest(0.0000001) -> "1e-7" 145 // ToShortest(0.0000001) -> "1e-7"
146 // ToShortest(111111111111111111111.0) -> "111111111111111110000" 146 // ToShortest(111111111111111111111.0) -> "111111111111111110000"
147 // ToShortest(100000000000000000000.0) -> "100000000000000000000" 147 // ToShortest(100000000000000000000.0) -> "100000000000000000000"
148 // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21" 148 // ToShortest(1111111111111111111111.0) -> "1.1111111111111111e+21"
149 // 149 //
150 // Note: the conversion may round the output if the returned string 150 // Note: the conversion may round the output if the returned string
151 // is accurate enough to uniquely identify the input-number. 151 // is accurate enough to uniquely identify the input-number.
152 // For example the most precise representation of the double 9e59 equals 152 // For example the most precise representation of the double 9e59 equals
153 // "899999999999999918767229449717619953810131273674690656206848", but 153 // "899999999999999918767229449717619953810131273674690656206848", but
154 // the converter will return the shorter (but still correct) "9e59". 154 // the converter will return the shorter (but still correct) "9e59".
155 // 155 //
156 // Returns true if the conversion succeeds. The conversion always succee ds 156 // Returns true if the conversion succeeds. The conversion always succeeds
157 // except when the input value is special and no infinity_symbol or 157 // except when the input value is special and no infinity_symbol or
158 // nan_symbol has been given to the constructor. 158 // nan_symbol has been given to the constructor.
159 bool ToShortest(double value, StringBuilder* result_builder) const; 159 bool ToShortest(double value, StringBuilder* result_builder) const;
160 160
161 161 // Computes a decimal representation with a fixed number of digits after the
162 // Computes a decimal representation with a fixed number of digits after the 162 // decimal point. The last emitted digit is rounded.
163 // decimal point. The last emitted digit is rounded. 163 //
164 // 164 // Examples:
165 // Examples: 165 // ToFixed(3.12, 1) -> "3.1"
166 // ToFixed(3.12, 1) -> "3.1" 166 // ToFixed(3.1415, 3) -> "3.142"
167 // ToFixed(3.1415, 3) -> "3.142" 167 // ToFixed(1234.56789, 4) -> "1234.5679"
168 // ToFixed(1234.56789, 4) -> "1234.5679" 168 // ToFixed(1.23, 5) -> "1.23000"
169 // ToFixed(1.23, 5) -> "1.23000" 169 // ToFixed(0.1, 4) -> "0.1000"
170 // ToFixed(0.1, 4) -> "0.1000" 170 // ToFixed(1e30, 2) -> "1000000000000000019884624838656.00"
171 // ToFixed(1e30, 2) -> "1000000000000000019884624838656.00" 171 // ToFixed(0.1, 30) -> "0.100000000000000005551115123126"
172 // ToFixed(0.1, 30) -> "0.100000000000000005551115123126" 172 // ToFixed(0.1, 17) -> "0.10000000000000001"
173 // ToFixed(0.1, 17) -> "0.10000000000000001" 173 //
174 // 174 // If requested_digits equals 0, then the tail of the result depends on
175 // If requested_digits equals 0, then the tail of the result depends on 175 // the EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT.
176 // the EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT. 176 // Examples, for requested_digits == 0,
177 // Examples, for requested_digits == 0, 177 // let EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT be
178 // let EMIT_TRAILING_DECIMAL_POINT and EMIT_TRAILING_ZERO_AFTER_POINT be 178 // - false and false: then 123.45 -> 123
179 // - false and false: then 123.45 -> 123 179 // 0.678 -> 1
180 // 0.678 -> 1 180 // - true and false: then 123.45 -> 123.
181 // - true and false: then 123.45 -> 123. 181 // 0.678 -> 1.
182 // 0.678 -> 1. 182 // - true and true: then 123.45 -> 123.0
183 // - true and true: then 123.45 -> 123.0 183 // 0.678 -> 1.0
184 // 0.678 -> 1.0 184 //
185 // 185 // Returns true if the conversion succeeds. The conversion always succeeds
186 // Returns true if the conversion succeeds. The conversion always succee ds 186 // except for the following cases:
187 // except for the following cases: 187 // - the input value is special and no infinity_symbol or nan_symbol has
188 // - the input value is special and no infinity_symbol or nan_symbol h as 188 // been provided to the constructor,
189 // been provided to the constructor, 189 // - 'value' > 10^kMaxFixedDigitsBeforePoint, or
190 // - 'value' > 10^kMaxFixedDigitsBeforePoint, or 190 // - 'requested_digits' > kMaxFixedDigitsAfterPoint.
191 // - 'requested_digits' > kMaxFixedDigitsAfterPoint. 191 // The last two conditions imply that the result will never contain more than
192 // The last two conditions imply that the result will never contain more than 192 // 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint characters
193 // 1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint charac ters 193 // (one additional character for the sign, and one for the decimal point).
194 // (one additional character for the sign, and one for the decimal point ). 194 bool ToFixed(double value,
195 bool ToFixed(double value, 195 int requested_digits,
196 StringBuilder* result_builder) const;
197
198 // Computes a representation in exponential format with requested_digits
199 // after the decimal point. The last emitted digit is rounded.
200 // If requested_digits equals -1, then the shortest exponential representation
201 // is computed.
202 //
203 // Examples with EMIT_POSITIVE_EXPONENT_SIGN deactivated, and
204 // exponent_character set to 'e'.
205 // ToExponential(3.12, 1) -> "3.1e0"
206 // ToExponential(5.0, 3) -> "5.000e0"
207 // ToExponential(0.001, 2) -> "1.00e-3"
208 // ToExponential(3.1415, -1) -> "3.1415e0"
209 // ToExponential(3.1415, 4) -> "3.1415e0"
210 // ToExponential(3.1415, 3) -> "3.142e0"
211 // ToExponential(123456789000000, 3) -> "1.235e14"
212 // ToExponential(1000000000000000019884624838656.0, -1) -> "1e30"
213 // ToExponential(1000000000000000019884624838656.0, 32) ->
214 // "1.00000000000000001988462483865600e30"
215 // ToExponential(1234, 0) -> "1e3"
216 //
217 // Returns true if the conversion succeeds. The conversion always succeeds
218 // except for the following cases:
219 // - the input value is special and no infinity_symbol or nan_symbol has
220 // been provided to the constructor,
221 // - 'requested_digits' > kMaxExponentialDigits.
222 // The last condition implies that the result will never contain more than
223 // kMaxExponentialDigits + 8 characters (the sign, the digit before the
224 // decimal point, the decimal point, the exponent character, the
225 // exponent's sign, and at most 3 exponent digits).
226 bool ToExponential(double value,
196 int requested_digits, 227 int requested_digits,
197 StringBuilder* result_builder) const; 228 StringBuilder* result_builder) const;
198 229
199 // Computes a representation in exponential format with requested_digits 230 // Computes 'precision' leading digits of the given 'value' and returns them
200 // after the decimal point. The last emitted digit is rounded. 231 // either in exponential or decimal format, depending on
201 // If requested_digits equals -1, then the shortest exponential represen tation 232 // max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the
202 // is computed. 233 // constructor).
203 // 234 // The last computed digit is rounded.
204 // Examples with EMIT_POSITIVE_EXPONENT_SIGN deactivated, and 235 //
205 // exponent_character set to 'e'. 236 // Example with max_leading_padding_zeroes_in_precision_mode = 6.
206 // ToExponential(3.12, 1) -> "3.1e0" 237 // ToPrecision(0.0000012345, 2) -> "0.0000012"
207 // ToExponential(5.0, 3) -> "5.000e0" 238 // ToPrecision(0.00000012345, 2) -> "1.2e-7"
208 // ToExponential(0.001, 2) -> "1.00e-3" 239 // Similarily the converter may add up to
209 // ToExponential(3.1415, -1) -> "3.1415e0" 240 // max_trailing_padding_zeroes_in_precision_mode in precision mode to avoid
210 // ToExponential(3.1415, 4) -> "3.1415e0" 241 // returning an exponential representation. A zero added by the
211 // ToExponential(3.1415, 3) -> "3.142e0" 242 // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit.
212 // ToExponential(123456789000000, 3) -> "1.235e14" 243 // Examples for max_trailing_padding_zeroes_in_precision_mode = 1:
213 // ToExponential(1000000000000000019884624838656.0, -1) -> "1e30" 244 // ToPrecision(230.0, 2) -> "230"
214 // ToExponential(1000000000000000019884624838656.0, 32) -> 245 // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT.
215 // "1.00000000000000001988462483865600e30" 246 // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POINT.
216 // ToExponential(1234, 0) -> "1e3" 247 // Examples for max_trailing_padding_zeroes_in_precision_mode = 3, and no
217 // 248 // EMIT_TRAILING_ZERO_AFTER_POINT:
218 // Returns true if the conversion succeeds. The conversion always succee ds 249 // ToPrecision(123450.0, 6) -> "123450"
219 // except for the following cases: 250 // ToPrecision(123450.0, 5) -> "123450"
220 // - the input value is special and no infinity_symbol or nan_symbol h as 251 // ToPrecision(123450.0, 4) -> "123500"
221 // been provided to the constructor, 252 // ToPrecision(123450.0, 3) -> "123000"
222 // - 'requested_digits' > kMaxExponentialDigits. 253 // ToPrecision(123450.0, 2) -> "1.2e5"
223 // The last condition implies that the result will never contain more th an 254 //
224 // kMaxExponentialDigits + 8 characters (the sign, the digit before the 255 // Returns true if the conversion succeeds. The conversion always succeeds
225 // decimal point, the decimal point, the exponent character, the 256 // except for the following cases:
226 // exponent's sign, and at most 3 exponent digits). 257 // - the input value is special and no infinity_symbol or nan_symbol has
227 bool ToExponential(double value, 258 // been provided to the constructor,
228 int requested_digits, 259 // - precision < kMinPericisionDigits
229 StringBuilder* result_builder) const; 260 // - precision > kMaxPrecisionDigits
230 261 // The last condition implies that the result will never contain more than
231 // Computes 'precision' leading digits of the given 'value' and returns them 262 // kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the
232 // either in exponential or decimal format, depending on 263 // exponent character, the exponent's sign, and at most 3 exponent digits).
233 // max_{leading|trailing}_padding_zeroes_in_precision_mode (given to the 264 bool ToPrecision(double value,
234 // constructor). 265 int precision,
235 // The last computed digit is rounded. 266 StringBuilder* result_builder) const;
236 // 267
237 // Example with max_leading_padding_zeroes_in_precision_mode = 6. 268 enum DtoaMode {
238 // ToPrecision(0.0000012345, 2) -> "0.0000012" 269 // Produce the shortest correct representation.
239 // ToPrecision(0.00000012345, 2) -> "1.2e-7" 270 // For example the output of 0.299999999999999988897 is (the less accurate
240 // Similarily the converter may add up to 271 // but correct) 0.3.
241 // max_trailing_padding_zeroes_in_precision_mode in precision mode to av oid 272 SHORTEST,
242 // returning an exponential representation. A zero added by the 273 // Produce a fixed number of digits after the decimal point.
243 // EMIT_TRAILING_ZERO_AFTER_POINT flag is counted for this limit. 274 // For instance fixed(0.1, 4) becomes 0.1000
244 // Examples for max_trailing_padding_zeroes_in_precision_mode = 1: 275 // If the input number is big, the output will be big.
245 // ToPrecision(230.0, 2) -> "230" 276 FIXED,
246 // ToPrecision(230.0, 2) -> "230." with EMIT_TRAILING_DECIMAL_POINT. 277 // Fixed number of digits (independent of the decimal point).
247 // ToPrecision(230.0, 2) -> "2.3e2" with EMIT_TRAILING_ZERO_AFTER_POIN T. 278 PRECISION
248 // Examples for max_trailing_padding_zeroes_in_precision_mode = 3, and n o 279 };
249 // EMIT_TRAILING_ZERO_AFTER_POINT: 280
250 // ToPrecision(123450.0, 6) -> "123450" 281 // The maximal number of digits that are needed to emit a double in base 10.
251 // ToPrecision(123450.0, 5) -> "123450" 282 // A higher precision can be achieved by using more digits, but the shortest
252 // ToPrecision(123450.0, 4) -> "123500" 283 // accurate representation of any double will never use more digits than
253 // ToPrecision(123450.0, 3) -> "123000" 284 // kBase10MaximalLength.
254 // ToPrecision(123450.0, 2) -> "1.2e5" 285 // Note that DoubleToAscii null-terminates its input. So the given buffer
255 // 286 // should be at least kBase10MaximalLength + 1 characters long.
256 // Returns true if the conversion succeeds. The conversion always succee ds 287 static const int kBase10MaximalLength = 17;
257 // except for the following cases: 288
258 // - the input value is special and no infinity_symbol or nan_symbol h as 289 // Converts the given double 'v' to ascii.
259 // been provided to the constructor, 290 // The result should be interpreted as buffer * 10^(point-length).
260 // - precision < kMinPericisionDigits 291 //
261 // - precision > kMaxPrecisionDigits 292 // The output depends on the given mode:
262 // The last condition implies that the result will never contain more th an 293 // - SHORTEST: produce the least amount of digits for which the internal
263 // kMaxPrecisionDigits + 7 characters (the sign, the decimal point, the 294 // identity requirement is still satisfied. If the digits are printed
264 // exponent character, the exponent's sign, and at most 3 exponent digit s). 295 // (together with the correct exponent) then reading this number will give
265 bool ToPrecision(double value, 296 // 'v' again. The buffer will choose the representation that is closest to
266 int precision, 297 // 'v'. If there are two at the same distance, than the one farther away
267 StringBuilder* result_builder) const; 298 // from 0 is chosen (halfway cases - ending with 5 - are rounded up).
268 299 // In this mode the 'requested_digits' parameter is ignored.
269 enum DtoaMode { 300 // - FIXED: produces digits necessary to print a given number with
270 // Produce the shortest correct representation. 301 // 'requested_digits' digits after the decimal point. The produced digits
271 // For example the output of 0.299999999999999988897 is (the less ac curate 302 // might be too short in which case the caller has to fill the remainder
272 // but correct) 0.3. 303 // with '0's.
273 SHORTEST, 304 // Example: toFixed(0.001, 5) is allowed to return buffer="1", point=-2.
274 // Produce a fixed number of digits after the decimal point. 305 // Halfway cases are rounded towards +/-Infinity (away from 0). The call
275 // For instance fixed(0.1, 4) becomes 0.1000 306 // toFixed(0.15, 2) thus returns buffer="2", point=0.
276 // If the input number is big, the output will be big. 307 // The returned buffer may contain digits that would be truncated from the
277 FIXED, 308 // shortest representation of the input.
278 // Fixed number of digits (independent of the decimal point). 309 // - PRECISION: produces 'requested_digits' where the first digit is not '0'.
279 PRECISION 310 // Even though the length of produced digits usually equals
280 }; 311 // 'requested_digits', the function is allowed to return fewer digits, in
281 312 // which case the caller has to fill the missing digits with '0's.
282 // The maximal number of digits that are needed to emit a double in base 10. 313 // Halfway cases are again rounded away from 0.
283 // A higher precision can be achieved by using more digits, but the shor test 314 // DoubleToAscii expects the given buffer to be big enough to hold all
284 // accurate representation of any double will never use more digits than 315 // digits and a terminating null-character. In SHORTEST-mode it expects a
285 // kBase10MaximalLength. 316 // buffer of at least kBase10MaximalLength + 1. In all other modes the
286 // Note that DoubleToAscii null-terminates its input. So the given buffe r 317 // requested_digits parameter (+ 1 for the null-character) limits the size of
287 // should be at least kBase10MaximalLength + 1 characters long. 318 // the output. The given length is only used in debug mode to ensure the
288 static const int kBase10MaximalLength = 17; 319 // buffer is big enough.
289 320 static void DoubleToAscii(double v,
290 // Converts the given double 'v' to ascii. 321 DtoaMode mode,
291 // The result should be interpreted as buffer * 10^(point-length). 322 int requested_digits,
292 // 323 char* buffer,
293 // The output depends on the given mode: 324 int buffer_length,
294 // - SHORTEST: produce the least amount of digits for which the interna l 325 bool* sign,
295 // identity requirement is still satisfied. If the digits are printed 326 int* length,
296 // (together with the correct exponent) then reading this number will give 327 int* point);
297 // 'v' again. The buffer will choose the representation that is closes t to 328
298 // 'v'. If there are two at the same distance, than the one farther aw ay 329 private:
299 // from 0 is chosen (halfway cases - ending with 5 - are rounded up). 330 // If the value is a special value (NaN or Infinity) constructs the
300 // In this mode the 'requested_digits' parameter is ignored. 331 // corresponding string using the configured infinity/nan-symbol.
301 // - FIXED: produces digits necessary to print a given number with 332 // If either of them is NULL or the value is not special then the
302 // 'requested_digits' digits after the decimal point. The produced dig its 333 // function returns false.
303 // might be too short in which case the caller has to fill the remaind er 334 bool HandleSpecialValues(double value, StringBuilder* result_builder) const;
304 // with '0's. 335 // Constructs an exponential representation (i.e. 1.234e56).
305 // Example: toFixed(0.001, 5) is allowed to return buffer="1", point=- 2. 336 // The given exponent assumes a decimal point after the first decimal digit.
306 // Halfway cases are rounded towards +/-Infinity (away from 0). The ca ll 337 void CreateExponentialRepresentation(const char* decimal_digits,
307 // toFixed(0.15, 2) thus returns buffer="2", point=0. 338 int length,
308 // The returned buffer may contain digits that would be truncated from the 339 int exponent,
309 // shortest representation of the input. 340 StringBuilder* result_builder) const;
310 // - PRECISION: produces 'requested_digits' where the first digit is no t '0'. 341 // Creates a decimal representation (i.e 1234.5678).
311 // Even though the length of produced digits usually equals 342 void CreateDecimalRepresentation(const char* decimal_digits,
312 // 'requested_digits', the function is allowed to return fewer digits, in 343 int length,
313 // which case the caller has to fill the missing digits with '0's. 344 int decimal_point,
314 // Halfway cases are again rounded away from 0. 345 int digits_after_point,
315 // DoubleToAscii expects the given buffer to be big enough to hold all 346 StringBuilder* result_builder) const;
316 // digits and a terminating null-character. In SHORTEST-mode it expects a 347
317 // buffer of at least kBase10MaximalLength + 1. In all other modes the 348 const int flags_;
318 // requested_digits parameter (+ 1 for the null-character) limits the si ze of 349 const char* const infinity_symbol_;
319 // the output. The given length is only used in debug mode to ensure the 350 const char* const nan_symbol_;
320 // buffer is big enough. 351 const char exponent_character_;
321 static void DoubleToAscii(double v, 352 const int decimal_in_shortest_low_;
322 DtoaMode mode, 353 const int decimal_in_shortest_high_;
323 int requested_digits, 354 const int max_leading_padding_zeroes_in_precision_mode_;
324 char* buffer, 355 const int max_trailing_padding_zeroes_in_precision_mode_;
325 int buffer_length, 356
326 bool* sign, 357 DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter);
327 int* length, 358 };
328 int* point); 359
329 360 class StringToDoubleConverter {
330 private: 361 public:
331 // If the value is a special value (NaN or Infinity) constructs the 362 // Performs the conversion.
332 // corresponding string using the configured infinity/nan-symbol. 363 // The output parameter 'processed_characters_count' is set to the number
333 // If either of them is NULL or the value is not special then the 364 // of characters that have been processed to read the number.
334 // function returns false. 365 static double StringToDouble(const char* buffer,
335 bool HandleSpecialValues(double value, StringBuilder* result_builder) co nst; 366 size_t length,
336 // Constructs an exponential representation (i.e. 1.234e56). 367 size_t* processed_characters_count);
337 // The given exponent assumes a decimal point after the first decimal di git. 368
338 void CreateExponentialRepresentation(const char* decimal_digits, 369 private:
339 int length, 370 DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter);
340 int exponent, 371 };
341 StringBuilder* result_builder) cons t;
342 // Creates a decimal representation (i.e 1234.5678).
343 void CreateDecimalRepresentation(const char* decimal_digits,
344 int length,
345 int decimal_point,
346 int digits_after_point,
347 StringBuilder* result_builder) const;
348
349 const int flags_;
350 const char* const infinity_symbol_;
351 const char* const nan_symbol_;
352 const char exponent_character_;
353 const int decimal_in_shortest_low_;
354 const int decimal_in_shortest_high_;
355 const int max_leading_padding_zeroes_in_precision_mode_;
356 const int max_trailing_padding_zeroes_in_precision_mode_;
357
358 DISALLOW_IMPLICIT_CONSTRUCTORS(DoubleToStringConverter);
359 };
360
361
362 class StringToDoubleConverter {
363 public:
364 // Performs the conversion.
365 // The output parameter 'processed_characters_count' is set to the numbe r
366 // of characters that have been processed to read the number.
367 static double StringToDouble(const char* buffer, size_t length, size_t* processed_characters_count);
368
369 private:
370 DISALLOW_IMPLICIT_CONSTRUCTORS(StringToDoubleConverter);
371 };
372 372
373 } // namespace double_conversion 373 } // namespace double_conversion
374 374
375 } // namespace WTF 375 } // namespace WTF
376 376
377 #endif // DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_ 377 #endif // DOUBLE_CONVERSION_DOUBLE_CONVERSION_H_
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/dtoa/double.h ('k') | third_party/WebKit/Source/wtf/dtoa/fast-dtoa.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698