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

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

Issue 8728006: Implement Double.toStringAsFixed. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Enable more tests and small fixes. Created 9 years 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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 } 101 }
102 102
103 double round() native "Double_round"; 103 double round() native "Double_round";
104 double floor() native "Double_floor"; 104 double floor() native "Double_floor";
105 double ceil () native "Double_ceil"; 105 double ceil () native "Double_ceil";
106 double truncate() native "Double_truncate"; 106 double truncate() native "Double_truncate";
107 int toInt() native "Double_toInt"; 107 int toInt() native "Double_toInt";
108 double toDouble() { return this; } 108 double toDouble() { return this; }
109 109
110 double pow(num exponent) { 110 double pow(num exponent) {
111 return pow_(exponent.toDouble()); 111 return _pow(exponent.toDouble());
112 } 112 }
113 double pow_(double exponent) native "Double_pow"; 113 double _pow(double exponent) native "Double_pow";
Ivan Posva 2011/12/20 00:30:03 Thanks!
114 114
115 String toStringAsFixed(int fractionDigits) { 115 String toStringAsFixed(int fractionDigits) {
116 // See ECMAScript-262, 15.7.4.5 for details. 116 // See ECMAScript-262, 15.7.4.5 for details.
117 117
118 // Step 2. 118 // Step 2.
119 if (fractionDigits < 0 || fractionDigits > 20) { 119 if (fractionDigits < 0 || fractionDigits > 20) {
120 // TODO(antonm): should be proper RangeError or Dart counterpart. 120 // TODO(antonm): should be proper RangeError or Dart counterpart.
121 throw "Range error"; 121 throw "Range error";
122 } 122 }
123 123
124 // Step 3. 124 // Step 3.
125 double x = this; 125 double x = this;
126 126
127 // Step 4. 127 // Step 4, 5 and 6 skipped. Will be dealt with by native function.
128 if (x.isNaN()) { 128
129 return "NaN"; 129 // Step 7.
130 if (x >= 1e21 || x <= -1e21) {
131 return x.toString();
130 } 132 }
131 133
132 // Step 5. 134 return _toStringAsFixed(fractionDigits);
133 String s = ""; 135 }
136 String _toStringAsFixed(int fractionDigits) native "Double_toStringAsFixed";
134 137
135 // Step 6. 138 String toStringAsExponential(int fractionDigits) {
136 if (x.isNegative() && x != 0.0) { 139 // See ECMAScript-262, 15.7.4.6 for details.
137 s = "-"; 140
138 x = -x; 141 // The EcmaScript specification checks for NaN and Infinity before looking
142 // at the fractionDigits. In Dart we are consistent with toStringAsFixed and
143 // look at the fractionDigits first.
144
145 // Step 7.
146 if (fractionDigits < 0 || fractionDigits > 20) {
147 // TODO(antonm): should be proper RangeError or Dart counterpart.
148 throw "Range error";
139 } 149 }
140 150
141 // Step 7. 151 return _toStringAsExponential(fractionDigits);
142 String m; 152 }
143 if (x > 10e21) { 153 String _toStringAsExponential(int fractionDigits)
144 m = x.toString(); 154 native "Double_toStringAsExponential";
145 } else {
146 // Step 8.
147 155
148 // Step 8.a. 156 String toStringAsPrecision(int precision) {
149 // This is an approximation for n from the standard. 157 // See ECMAScript-262, 15.7.4.7 for details.
150 int n = (x * (10.0).pow(fractionDigits)).round().toInt();
151 158
152 // Step 8.b. 159 // The EcmaScript specification checks for NaN and Infinity before looking
153 m = n.toString(); 160 // at the fractionDigits. In Dart we are consistent with toStringAsFixed and
161 // look at the fractionDigits first.
154 162
155 // Step 8.c. 163 // Step 8.
156 if (fractionDigits != 0) { 164 if (precision < 1 || precision > 21) {
157 // Step 8.c.i. 165 // TODO(antonm): should be proper RangeError or Dart counterpart.
158 int k = m.length; 166 throw "Range error";
159 // Step 8.c.ii.
160 if (k <= fractionDigits) {
161 StringBuffer sb = new StringBuffer();
162 for (int i = 0; i < fractionDigits + 1 - k; i++) {
163 sb.add("0");
164 }
165 String z = sb.toString();
166 m = z + m;
167 k = fractionDigits + 1;
168 }
169 // Step 8.c.iii.
170 String a = m.substring(0, k - fractionDigits);
171 String b = m.substring(k - fractionDigits);
172 // Step 8.c.iv.
173 m = a + "." + b;
174 }
175 } 167 }
176 168
177 // Step 9. 169 return _toStringAsPrecision(precision);
178 return s + m;
179 } 170 }
180 String toStringAsExponential(int fractionDigits) { 171 String _toStringAsPrecision(int fractionDigits)
181 throw "Double.toStringAsExponential unimplemented."; 172 native "Double_toStringAsPrecision";
182 } 173
183 String toStringAsPrecision(int precision) {
184 throw "Double.toStringAsPrecision unimplemented.";
185 }
186 String toRadixString(int radix) { 174 String toRadixString(int radix) {
187 throw "Double.toRadixString unimplemented."; 175 throw "Double.toRadixString unimplemented.";
188 } 176 }
189 177
190 // Order is: NaN > Infinity > ... > 0.0 > -0.0 > ... > -Infinity. 178 // Order is: NaN > Infinity > ... > 0.0 > -0.0 > ... > -Infinity.
191 int compareTo(Comparable other) { 179 int compareTo(Comparable other) {
192 final int EQUAL = 0, LESS = -1, GREATER = 1; 180 final int EQUAL = 0, LESS = -1, GREATER = 1;
193 if (this < other) { 181 if (this < other) {
194 return LESS; 182 return LESS;
195 } else if (this > other) { 183 } else if (this > other) {
(...skipping 10 matching lines...) Expand all
206 return EQUAL; 194 return EQUAL;
207 } 195 }
208 } else if (isNaN()) { 196 } else if (isNaN()) {
209 return other.isNaN() ? EQUAL : GREATER; 197 return other.isNaN() ? EQUAL : GREATER;
210 } else { 198 } else {
211 // Other is NaN. 199 // Other is NaN.
212 return LESS; 200 return LESS;
213 } 201 }
214 } 202 }
215 } 203 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698