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

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

Issue 9113043: Implement Double.{toString, toStringAsExponential, toStringAsPrecision} (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revert mintmaker changes. Created 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 int hashCode() { 8 int hashCode() {
9 try { 9 try {
10 return toInt(); 10 return toInt();
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 String _toStringAsFixed(int fractionDigits) native "Double_toStringAsFixed"; 139 String _toStringAsFixed(int fractionDigits) native "Double_toStringAsFixed";
140 140
141 String toStringAsExponential(int fractionDigits) { 141 String toStringAsExponential(int fractionDigits) {
142 // See ECMAScript-262, 15.7.4.6 for details. 142 // See ECMAScript-262, 15.7.4.6 for details.
143 143
144 // The EcmaScript specification checks for NaN and Infinity before looking 144 // The EcmaScript specification checks for NaN and Infinity before looking
145 // at the fractionDigits. In Dart we are consistent with toStringAsFixed and 145 // at the fractionDigits. In Dart we are consistent with toStringAsFixed and
146 // look at the fractionDigits first. 146 // look at the fractionDigits first.
147 147
148 // Step 7. 148 // Step 7.
149 if (fractionDigits < 0 || fractionDigits > 20) { 149 if (fractionDigits !== null &&
150 (fractionDigits < 0 || fractionDigits > 20)) {
150 // TODO(antonm): should be proper RangeError or Dart counterpart. 151 // TODO(antonm): should be proper RangeError or Dart counterpart.
151 throw "Range error"; 152 throw "Range error";
152 } 153 }
153 154
155 if (isNaN()) return "NaN";
156 if (this == double.INFINITY) return "Infinity";
157 if (this == -double.INFINITY) return "-Infinity";
158
159 // The dart function prints the shortest representation when fractionDigits
160 // equals null. The native function wants -1 instead.
161 fractionDigits = (fractionDigits === null) ? -1 : fractionDigits;
162
154 return _toStringAsExponential(fractionDigits); 163 return _toStringAsExponential(fractionDigits);
155 } 164 }
156 String _toStringAsExponential(int fractionDigits) 165 String _toStringAsExponential(int fractionDigits)
157 native "Double_toStringAsExponential"; 166 native "Double_toStringAsExponential";
158 167
159 String toStringAsPrecision(int precision) { 168 String toStringAsPrecision(int precision) {
160 // See ECMAScript-262, 15.7.4.7 for details. 169 // See ECMAScript-262, 15.7.4.7 for details.
161 170
162 // The EcmaScript specification checks for NaN and Infinity before looking 171 // The EcmaScript specification checks for NaN and Infinity before looking
163 // at the fractionDigits. In Dart we are consistent with toStringAsFixed and 172 // at the fractionDigits. In Dart we are consistent with toStringAsFixed and
164 // look at the fractionDigits first. 173 // look at the fractionDigits first.
165 174
166 // Step 8. 175 // Step 8.
167 if (precision < 1 || precision > 21) { 176 if (precision < 1 || precision > 21) {
168 // TODO(antonm): should be proper RangeError or Dart counterpart. 177 // TODO(antonm): should be proper RangeError or Dart counterpart.
169 throw "Range error"; 178 throw "Range error";
170 } 179 }
171 180
181 if (isNaN()) return "NaN";
182 if (this == double.INFINITY) return "Infinity";
183 if (this == -double.INFINITY) return "-Infinity";
184
172 return _toStringAsPrecision(precision); 185 return _toStringAsPrecision(precision);
173 } 186 }
174 String _toStringAsPrecision(int fractionDigits) 187 String _toStringAsPrecision(int fractionDigits)
175 native "Double_toStringAsPrecision"; 188 native "Double_toStringAsPrecision";
176 189
177 String toRadixString(int radix) { 190 String toRadixString(int radix) {
178 throw "Double.toRadixString unimplemented."; 191 throw "Double.toRadixString unimplemented.";
179 } 192 }
180 193
181 // Order is: NaN > Infinity > ... > 0.0 > -0.0 > ... > -Infinity. 194 // Order is: NaN > Infinity > ... > 0.0 > -0.0 > ... > -Infinity.
(...skipping 15 matching lines...) Expand all
197 return EQUAL; 210 return EQUAL;
198 } 211 }
199 } else if (isNaN()) { 212 } else if (isNaN()) {
200 return other.isNaN() ? EQUAL : GREATER; 213 return other.isNaN() ? EQUAL : GREATER;
201 } else { 214 } else {
202 // Other is NaN. 215 // Other is NaN.
203 return LESS; 216 return LESS;
204 } 217 }
205 } 218 }
206 } 219 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698