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

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

Issue 1211473002: Make int.gcd accept zero operands. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Address comment. Created 5 years, 5 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 | « runtime/lib/bigint.dart ('k') | sdk/lib/_internal/js_runtime/lib/js_number.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 extends _Num { 7 class _IntegerImplementation extends _Num {
8 // The Dart class _Bigint extending _IntegerImplementation requires a 8 // The Dart class _Bigint extending _IntegerImplementation requires a
9 // default constructor. 9 // default constructor.
10 10
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 } 383 }
384 int t = this; 384 int t = this;
385 if ((t < 0) || (t >= m)) t %= m; 385 if ((t < 0) || (t >= m)) t %= m;
386 if (t == 1) return 1; 386 if (t == 1) return 1;
387 if ((t == 0) || (t.isEven && m.isEven)) { 387 if ((t == 0) || (t.isEven && m.isEven)) {
388 throw new Exception("Not coprime"); 388 throw new Exception("Not coprime");
389 } 389 }
390 return _binaryGcd(m, t, true); 390 return _binaryGcd(m, t, true);
391 } 391 }
392 392
393 // Returns gcd of abs(this) and abs(other), with this != 0 and other !=0. 393 // Returns gcd of abs(this) and abs(other).
394 int gcd(int other) { 394 int gcd(int other) {
395 if (other is! int) { 395 if (other is! int) {
396 throw new ArgumentError.value(other, "other", "not an integer"); 396 throw new ArgumentError.value(other, "other", "not an integer");
397 } 397 }
398 if (this == 0) {
399 throw new ArgumentError.value(this, "this", "must not be zero");
400 }
401 if (other == 0) {
402 throw new ArgumentError.value(this, "other", "must not be zero");
403 }
404 int x = this.abs(); 398 int x = this.abs();
405 int y = other.abs(); 399 int y = other.abs();
400 if (x == 0) return y;
401 if (y == 0) return x;
406 if ((x == 1) || (y == 1)) return 1; 402 if ((x == 1) || (y == 1)) return 1;
407 if (other is _Bigint) { 403 if (other is _Bigint) {
408 return _toBigint().gcd(other); 404 return _toBigint().gcd(other);
409 } 405 }
410 return _binaryGcd(x, y, false); 406 return _binaryGcd(x, y, false);
411 } 407 }
412 } 408 }
413 409
414 class _Smi extends _IntegerImplementation implements int { 410 class _Smi extends _IntegerImplementation implements int {
415 factory _Smi._uninstantiable() { 411 factory _Smi._uninstantiable() {
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
623 // Shift by mint exceeds range that can be handled by the VM. 619 // Shift by mint exceeds range that can be handled by the VM.
624 int _shrFromInt(int other) { 620 int _shrFromInt(int other) {
625 if (other < 0) { 621 if (other < 0) {
626 return -1; 622 return -1;
627 } else { 623 } else {
628 return 0; 624 return 0;
629 } 625 }
630 } 626 }
631 int _shlFromInt(int other) native "Mint_shlFromInt"; 627 int _shlFromInt(int other) native "Mint_shlFromInt";
632 } 628 }
OLDNEW
« no previous file with comments | « runtime/lib/bigint.dart ('k') | sdk/lib/_internal/js_runtime/lib/js_number.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698