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

Side by Side Diff: runtime/lib/double.dart

Issue 1985823002: Cleanup various argument and range errors in double.dart and integers.dart. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: RangeError.range takes minValue and maxValue of type int, not num. Created 4 years, 7 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
« no previous file with comments | « no previous file | runtime/lib/integers.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 class _Double implements double { 5 class _Double implements double {
6 factory _Double.fromInteger(int value) 6 factory _Double.fromInteger(int value)
7 native "Double_doubleFromInteger"; 7 native "Double_doubleFromInteger";
8 8
9 Type get runtimeType => double; 9 Type get runtimeType => double;
10 10
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 int floor() => floorToDouble().toInt(); 110 int floor() => floorToDouble().toInt();
111 int ceil () => ceilToDouble().toInt(); 111 int ceil () => ceilToDouble().toInt();
112 int truncate() => truncateToDouble().toInt(); 112 int truncate() => truncateToDouble().toInt();
113 113
114 double roundToDouble() native "Double_round"; 114 double roundToDouble() native "Double_round";
115 double floorToDouble() native "Double_floor"; 115 double floorToDouble() native "Double_floor";
116 double ceilToDouble() native "Double_ceil"; 116 double ceilToDouble() native "Double_ceil";
117 double truncateToDouble() native "Double_truncate"; 117 double truncateToDouble() native "Double_truncate";
118 118
119 num clamp(num lowerLimit, num upperLimit) { 119 num clamp(num lowerLimit, num upperLimit) {
120 if (lowerLimit is! num) throw new ArgumentError(lowerLimit); 120 if (lowerLimit is! num) {
121 if (upperLimit is! num) throw new ArgumentError(upperLimit); 121 throw new ArgumentError.value(lowerLimit, "lowerLimit", "not a number");
122 }
123 if (upperLimit is! num) {
124 throw new ArgumentError.value(upperLimit, "upperLimit", "not a number");
125 }
122 126
123 if (lowerLimit.compareTo(upperLimit) > 0) { 127 if (lowerLimit.compareTo(upperLimit) > 0) {
124 throw new ArgumentError(lowerLimit); 128 throw new ArgumentError(lowerLimit);
125 } 129 }
126 if (lowerLimit.isNaN) return lowerLimit; 130 if (lowerLimit.isNaN) return lowerLimit;
127 if (this.compareTo(lowerLimit) < 0) return lowerLimit; 131 if (this.compareTo(lowerLimit) < 0) return lowerLimit;
128 if (this.compareTo(upperLimit) > 0) return upperLimit; 132 if (this.compareTo(upperLimit) > 0) return upperLimit;
129 return this; 133 return this;
130 } 134 }
131 135
(...skipping 27 matching lines...) Expand all
159 _cache[_cacheEvictIndex] = this; 163 _cache[_cacheEvictIndex] = this;
160 _cache[_cacheEvictIndex + 1] = result; 164 _cache[_cacheEvictIndex + 1] = result;
161 _cacheEvictIndex = (_cacheEvictIndex + 2) & CACHE_MASK; 165 _cacheEvictIndex = (_cacheEvictIndex + 2) & CACHE_MASK;
162 return result; 166 return result;
163 } 167 }
164 168
165 String toStringAsFixed(int fractionDigits) { 169 String toStringAsFixed(int fractionDigits) {
166 // See ECMAScript-262, 15.7.4.5 for details. 170 // See ECMAScript-262, 15.7.4.5 for details.
167 171
168 if (fractionDigits is! int) { 172 if (fractionDigits is! int) {
169 throw new ArgumentError(fractionDigits); 173 throw new ArgumentError.value(
174 fractionDigits, "fractionDigits", "not an integer");
170 } 175 }
171 // Step 2. 176 // Step 2.
172 if (fractionDigits < 0 || fractionDigits > 20) { 177 if (fractionDigits < 0 || fractionDigits > 20) {
173 throw new RangeError(fractionDigits); 178 throw new RangeError.range(fractionDigits, 0, 20, "fractionDigits");
174 } 179 }
175 180
176 // Step 3. 181 // Step 3.
177 double x = this; 182 double x = this;
178 183
179 // Step 4. 184 // Step 4.
180 if (isNaN) return "NaN"; 185 if (isNaN) return "NaN";
181 186
182 // Step 5 and 6 skipped. Will be dealt with by native function. 187 // Step 5 and 6 skipped. Will be dealt with by native function.
183 188
184 // Step 7. 189 // Step 7.
185 if (x >= 1e21 || x <= -1e21) { 190 if (x >= 1e21 || x <= -1e21) {
186 return x.toString(); 191 return x.toString();
187 } 192 }
188 193
189 return _toStringAsFixed(fractionDigits); 194 return _toStringAsFixed(fractionDigits);
190 } 195 }
191 String _toStringAsFixed(int fractionDigits) native "Double_toStringAsFixed"; 196 String _toStringAsFixed(int fractionDigits) native "Double_toStringAsFixed";
192 197
193 String toStringAsExponential([int fractionDigits]) { 198 String toStringAsExponential([int fractionDigits]) {
194 // See ECMAScript-262, 15.7.4.6 for details. 199 // See ECMAScript-262, 15.7.4.6 for details.
195 200
196 // The EcmaScript specification checks for NaN and Infinity before looking 201 // The EcmaScript specification checks for NaN and Infinity before looking
197 // at the fractionDigits. In Dart we are consistent with toStringAsFixed and 202 // at the fractionDigits. In Dart we are consistent with toStringAsFixed and
198 // look at the fractionDigits first. 203 // look at the fractionDigits first.
199 204
200 // Step 7. 205 // Step 7.
201 if (fractionDigits != null) { 206 if (fractionDigits != null) {
202 if (fractionDigits is! int) { 207 if (fractionDigits is! int) {
203 throw new ArgumentError(fractionDigits); 208 throw new ArgumentError.value(
209 fractionDigits, "fractionDigits", "not an integer");
204 } 210 }
205 if (fractionDigits < 0 || fractionDigits > 20) { 211 if (fractionDigits < 0 || fractionDigits > 20) {
206 throw new RangeError(fractionDigits); 212 throw new RangeError.range(fractionDigits, 0, 20, "fractionDigits");
207 } 213 }
208 } 214 }
209 215
210 if (isNaN) return "NaN"; 216 if (isNaN) return "NaN";
211 if (this == double.INFINITY) return "Infinity"; 217 if (this == double.INFINITY) return "Infinity";
212 if (this == -double.INFINITY) return "-Infinity"; 218 if (this == -double.INFINITY) return "-Infinity";
213 219
214 // The dart function prints the shortest representation when fractionDigits 220 // The dart function prints the shortest representation when fractionDigits
215 // equals null. The native function wants -1 instead. 221 // equals null. The native function wants -1 instead.
216 fractionDigits = (fractionDigits == null) ? -1 : fractionDigits; 222 fractionDigits = (fractionDigits == null) ? -1 : fractionDigits;
217 223
218 return _toStringAsExponential(fractionDigits); 224 return _toStringAsExponential(fractionDigits);
219 } 225 }
220 String _toStringAsExponential(int fractionDigits) 226 String _toStringAsExponential(int fractionDigits)
221 native "Double_toStringAsExponential"; 227 native "Double_toStringAsExponential";
222 228
223 String toStringAsPrecision(int precision) { 229 String toStringAsPrecision(int precision) {
224 // See ECMAScript-262, 15.7.4.7 for details. 230 // See ECMAScript-262, 15.7.4.7 for details.
225 231
226 // The EcmaScript specification checks for NaN and Infinity before looking 232 // The EcmaScript specification checks for NaN and Infinity before looking
227 // at the fractionDigits. In Dart we are consistent with toStringAsFixed and 233 // at the fractionDigits. In Dart we are consistent with toStringAsFixed and
228 // look at the fractionDigits first. 234 // look at the fractionDigits first.
229 235
230 if (precision is! int) throw new ArgumentError(precision); 236 if (precision is! int) {
231 237 throw new ArgumentError.value(precision, "precision", "not an integer");
238 }
232 // Step 8. 239 // Step 8.
233 if (precision < 1 || precision > 21) { 240 if (precision < 1 || precision > 21) {
234 throw new RangeError(precision); 241 throw new RangeError.range(precision, 1, 21, "precision");
235 } 242 }
236 243
237 if (isNaN) return "NaN"; 244 if (isNaN) return "NaN";
238 if (this == double.INFINITY) return "Infinity"; 245 if (this == double.INFINITY) return "Infinity";
239 if (this == -double.INFINITY) return "-Infinity"; 246 if (this == -double.INFINITY) return "-Infinity";
240 247
241 return _toStringAsPrecision(precision); 248 return _toStringAsPrecision(precision);
242 } 249 }
243 String _toStringAsPrecision(int fractionDigits) 250 String _toStringAsPrecision(int fractionDigits)
244 native "Double_toStringAsPrecision"; 251 native "Double_toStringAsPrecision";
(...skipping 17 matching lines...) Expand all
262 return EQUAL; 269 return EQUAL;
263 } 270 }
264 } else if (isNaN) { 271 } else if (isNaN) {
265 return other.isNaN ? EQUAL : GREATER; 272 return other.isNaN ? EQUAL : GREATER;
266 } else { 273 } else {
267 // Other is NaN. 274 // Other is NaN.
268 return LESS; 275 return LESS;
269 } 276 }
270 } 277 }
271 } 278 }
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/integers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698