OLD | NEW |
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 // TODO(srdjan): fix limitations. | 5 // TODO(srdjan): fix limitations. |
6 // - shift amount must be a Smi. | 6 // - shift amount must be a Smi. |
7 class _IntegerImplementation { | 7 class _IntegerImplementation { |
8 factory _IntegerImplementation._uninstantiable() { | 8 factory _IntegerImplementation._uninstantiable() { |
9 throw new UnsupportedError( | 9 throw new UnsupportedError( |
10 "_IntegerImplementation can only be allocated by the VM"); | 10 "_IntegerImplementation can only be allocated by the VM"); |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 // Note that we don't need to care for -0.0 for the lower limit. | 157 // Note that we don't need to care for -0.0 for the lower limit. |
158 if (this < lowerLimit) return lowerLimit; | 158 if (this < lowerLimit) return lowerLimit; |
159 if (this.compareTo(upperLimit) > 0) return upperLimit; | 159 if (this.compareTo(upperLimit) > 0) return upperLimit; |
160 return this; | 160 return this; |
161 } | 161 } |
162 | 162 |
163 int toInt() { return this; } | 163 int toInt() { return this; } |
164 double toDouble() { return new _Double.fromInteger(this); } | 164 double toDouble() { return new _Double.fromInteger(this); } |
165 | 165 |
166 int pow(int exponent) { | 166 int pow(int exponent) { |
167 double res = this.toDouble().pow(exponent); | 167 // Exponentiation by squaring. |
168 if (res.isInfinite) { | 168 int base = this; |
169 // Use Bigint instead. | 169 int result = 1; |
170 throw "_IntegerImplementation.pow not implemented for large integers."; | 170 while (exponent != 0) { |
| 171 if ((exponent & 1) == 1) { |
| 172 result *= base; |
| 173 } |
| 174 exponent >>= 1; |
| 175 base *= base; |
171 } | 176 } |
172 return res.toInt(); | 177 return result; |
173 } | 178 } |
174 | 179 |
175 String toStringAsFixed(int fractionDigits) { | 180 String toStringAsFixed(int fractionDigits) { |
176 return this.toDouble().toStringAsFixed(fractionDigits); | 181 return this.toDouble().toStringAsFixed(fractionDigits); |
177 } | 182 } |
178 String toStringAsExponential([int fractionDigits]) { | 183 String toStringAsExponential([int fractionDigits]) { |
179 return this.toDouble().toStringAsExponential(fractionDigits); | 184 return this.toDouble().toStringAsExponential(fractionDigits); |
180 } | 185 } |
181 String toStringAsPrecision(int precision) { | 186 String toStringAsPrecision(int precision) { |
182 return this.toDouble().toStringAsPrecision(precision); | 187 return this.toDouble().toStringAsPrecision(precision); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 } | 299 } |
295 } | 300 } |
296 int _shlFromInt(int other) { | 301 int _shlFromInt(int other) { |
297 throw const OutOfMemoryError(); | 302 throw const OutOfMemoryError(); |
298 } | 303 } |
299 | 304 |
300 int pow(int exponent) { | 305 int pow(int exponent) { |
301 throw "Bigint.pow not implemented"; | 306 throw "Bigint.pow not implemented"; |
302 } | 307 } |
303 } | 308 } |
OLD | NEW |