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

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

Issue 14619027: Fix for issue 10534. Implement int.pow correctly. Note that int.pow is considerably slower than dou… (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | runtime/lib/math_patch.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 // 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
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
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 }
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/math_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698