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

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

Issue 1211473002: Make int.gcd accept zero operands. (Closed) Base URL: https://github.com/dart-lang/sdk.git@master
Patch Set: Created 5 years, 6 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 | « no previous file | runtime/lib/integers.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) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 // Copyright 2009 The Go Authors. All rights reserved. 5 // Copyright 2009 The Go Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style 6 // Use of this source code is governed by a BSD-style
7 // license that can be found in the LICENSE file. 7 // license that can be found in the LICENSE file.
8 8
9 /* 9 /*
10 * Copyright (c) 2003-2005 Tom Wu 10 * Copyright (c) 2003-2005 Tom Wu
(...skipping 1540 matching lines...) Expand 10 before | Expand all | Expand 10 after
1551 final m_len = m_used + (m_used & 1); 1551 final m_len = m_used + (m_used & 1);
1552 x_digits = _cloneDigits(x_digits, 0, x_used, m_len); 1552 x_digits = _cloneDigits(x_digits, 0, x_used, m_len);
1553 y_digits = _cloneDigits(y_digits, 0, y_used, m_len); 1553 y_digits = _cloneDigits(y_digits, 0, y_used, m_len);
1554 int s = 0; 1554 int s = 0;
1555 if (inv) { 1555 if (inv) {
1556 if ((y_used == 1) && (y_digits[0] == 1)) return 1; 1556 if ((y_used == 1) && (y_digits[0] == 1)) return 1;
1557 if ((y_used == 0) || (y_digits[0].isEven && x_digits[0].isEven)) { 1557 if ((y_used == 0) || (y_digits[0].isEven && x_digits[0].isEven)) {
1558 throw new UnsupportedError("Not coprime"); 1558 throw new UnsupportedError("Not coprime");
1559 } 1559 }
1560 } else { 1560 } else {
1561 if (x_used == 0) { 1561 if ((x_used == 0) && (y_used == 0)) {
1562 throw new ArgumentError.value(0, "first operand", "must not be zero"); 1562 throw new ArgumentError.value(0, null,
1563 } 1563 "at least one operand must not be zero");
floitsch 2015/06/24 12:18:13 Wolfram Alpha goes as far as to say that gcd(0, 0)
Lasse Reichstein Nielsen 2015/06/24 12:44:10 It's a usable definition, as long as you can expla
1564 if (y_used == 0) {
1565 throw new ArgumentError.value(0, "second operand", "must not be zero");
1566 } 1564 }
1567 if (((x_used == 1) && (x_digits[0] == 1)) || 1565 if (((x_used == 1) && (x_digits[0] == 1)) ||
1568 ((y_used == 1) && (y_digits[0] == 1))) return 1; 1566 ((y_used == 1) && (y_digits[0] == 1))) return 1;
1569 bool xy_cloned = false; 1567 bool xy_cloned = false;
1570 while (x.isEven && y.isEven) { 1568 while (x.isEven && y.isEven) {
1571 _rsh(x_digits, x_used, 1, x_digits); 1569 _rsh(x_digits, x_used, 1, x_digits);
1572 _rsh(y_digits, y_used, 1, y_digits); 1570 _rsh(y_digits, y_used, 1, y_digits);
1573 s++; 1571 s++;
1574 } 1572 }
1575 if (s >= _DIGIT_BITS) { 1573 if (s >= _DIGIT_BITS) {
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
1798 if (m == 1) return 0; 1796 if (m == 1) return 0;
1799 m = m._toBigint(); 1797 m = m._toBigint();
1800 var t = this; 1798 var t = this;
1801 if (t._neg || (t._absCompare(m) >= 0)) { 1799 if (t._neg || (t._absCompare(m) >= 0)) {
1802 t %= m; 1800 t %= m;
1803 t = t._toBigint(); 1801 t = t._toBigint();
1804 } 1802 }
1805 return _binaryGcd(m, t, true); 1803 return _binaryGcd(m, t, true);
1806 } 1804 }
1807 1805
1808 // Returns gcd of abs(this) and abs(other), with this != 0 and other !=0. 1806 // Returns gcd of abs(this) and abs(other), with this != 0 and other !=0.
floitsch 2015/06/24 12:18:13 comment is not correct anymore.
Lasse Reichstein Nielsen 2015/06/24 12:44:10 Good catch.
1809 int gcd(int other) { 1807 int gcd(int other) {
1810 if (other is! int) { 1808 if (other is! int) {
1811 throw new ArgumentError.value(other, "other", "not an integer"); 1809 throw new ArgumentError.value(other, "other", "not an integer");
1812 } 1810 }
1811 if (other == 0) {
floitsch 2015/06/24 12:18:13 Are we sure that bigints cannot be 0? (If yes asse
Lasse Reichstein Nielsen 2015/06/24 12:44:10 They can be 0, at least internally. The `other._to
regis 2015/06/24 16:07:16 Correct. At this point, 'other' has not yet been f
1812 return this.abs();
1813 }
1813 return _binaryGcd(this, other._toBigint(), false); 1814 return _binaryGcd(this, other._toBigint(), false);
1814 } 1815 }
1815 } 1816 }
1816 1817
1817 // Interface for modular reduction. 1818 // Interface for modular reduction.
1818 class _Reduction { 1819 class _Reduction {
1819 // Return the number of digits used by r_digits. 1820 // Return the number of digits used by r_digits.
1820 int _convert(_Bigint x, Uint32List r_digits); 1821 int _convert(_Bigint x, Uint32List r_digits);
1821 int _mul(Uint32List x_digits, int x_used, 1822 int _mul(Uint32List x_digits, int x_used,
1822 Uint32List y_digits, int y_used, Uint32List r_digits); 1823 Uint32List y_digits, int y_used, Uint32List r_digits);
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
2081 2082
2082 int _mul(Uint32List x_digits, int x_used, 2083 int _mul(Uint32List x_digits, int x_used,
2083 Uint32List y_digits, int y_used, 2084 Uint32List y_digits, int y_used,
2084 Uint32List r_digits) { 2085 Uint32List r_digits) {
2085 var r_used = _Bigint._mulDigits(x_digits, x_used, 2086 var r_used = _Bigint._mulDigits(x_digits, x_used,
2086 y_digits, y_used, 2087 y_digits, y_used,
2087 r_digits); 2088 r_digits);
2088 return _reduce(r_digits, r_used); 2089 return _reduce(r_digits, r_used);
2089 } 2090 }
2090 } 2091 }
OLDNEW
« no previous file with comments | « no previous file | runtime/lib/integers.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698