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

Unified Diff: runtime/lib/bigint.dart

Issue 1209523002: Make modInv throw Exception on incompatible 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | runtime/lib/integers.dart » ('j') | runtime/lib/integers.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/bigint.dart
diff --git a/runtime/lib/bigint.dart b/runtime/lib/bigint.dart
index adfb0db4cca73f924008dec248d1c07a09a464d5..2e659bc76f9b1e8a7962f1db8fe358a39015deb3 100644
--- a/runtime/lib/bigint.dart
+++ b/runtime/lib/bigint.dart
@@ -1220,7 +1220,7 @@ class _Bigint extends _IntegerImplementation implements int {
// This method must support smi._toBigint()._shrFromInt(int).
int _shrFromInt(int other) {
if (_used == 0) return other; // Shift amount is zero.
- if (_neg) throw new RangeError(this);
+ if (_neg) throw new RangeError.range(this, 0, null);
assert(_DIGIT_BITS == 32); // Otherwise this code needs to be revised.
var shift;
if ((_used > 2) || ((_used == 2) && (_digits[1] > 0x10000000))) {
@@ -1239,7 +1239,7 @@ class _Bigint extends _IntegerImplementation implements int {
// An out of memory exception is thrown if the result cannot be allocated.
int _shlFromInt(int other) {
if (_used == 0) return other; // Shift amount is zero.
- if (_neg) throw new RangeError(this);
+ if (_neg) throw new RangeError.range(this, 0, null);
assert(_DIGIT_BITS == 32); // Otherwise this code needs to be revised.
var shift;
if (_used > 2 || (_used == 2 && _digits[1] > 0x10000000)) {
@@ -1397,10 +1397,10 @@ class _Bigint extends _IntegerImplementation implements int {
// Returns pow(this, e) % m, with e >= 0, m > 0.
int modPow(int e, int m) {
- if (e is! int) throw new ArgumentError(e);
- if (m is! int) throw new ArgumentError(m);
- if (e < 0) throw new RangeError(e);
- if (m <= 0) throw new RangeError(m);
+ if (e is! int) throw new ArgumentError.value(e, "exponent");
regis 2015/06/24 15:40:43 Below, you added "not an integer"
Lasse Reichstein Nielsen 2015/06/30 05:50:47 Done.
+ if (m is! int) throw new ArgumentError.value(m, "modulus");
regis 2015/06/24 15:40:43 ditto
Lasse Reichstein Nielsen 2015/06/30 05:50:47 Done.
+ if (e < 0) throw new RangeError.range(e, 0, null, "exponent");
+ if (m <= 0) throw new RangeError.range(m, 1, null, "modulus");
if (e == 0) return 1;
m = m._toBigint();
final m_used = m._used;
@@ -1541,7 +1541,7 @@ class _Bigint extends _IntegerImplementation implements int {
// If inv is false, returns gcd(x, y).
// If inv is true and gcd(x, y) = 1, returns d, so that c*x + d*y = 1.
- // If inv is true and gcd(x, y) != 1, throws RangeError("Not coprime").
+ // If inv is true and gcd(x, y) != 1, throws UnsupportedError("Not coprime").
static int _binaryGcd(_Bigint x, _Bigint y, bool inv) {
var x_digits = x._digits;
var y_digits = y._digits;
@@ -1555,10 +1555,15 @@ class _Bigint extends _IntegerImplementation implements int {
if (inv) {
if ((y_used == 1) && (y_digits[0] == 1)) return 1;
if ((y_used == 0) || (y_digits[0].isEven && x_digits[0].isEven)) {
- throw new RangeError("Not coprime");
+ throw new UnsupportedError("Not coprime");
}
} else {
- if ((x_used == 0) || (y_used == 0)) throw new RangeError(0);
+ if (x_used == 0) {
+ throw new ArgumentError.value(0, "first operand", "must not be zero");
regis 2015/06/24 15:40:43 x is 'this' at this point, i.e. the receiver for g
Lasse Reichstein Nielsen 2015/06/25 07:22:21 I wasn't able to convince myself that this was alw
+ }
+ if (y_used == 0) {
+ throw new ArgumentError.value(0, "second operand", "must not be zero");
regis 2015/06/24 15:40:43 y is the 'other' argument of gcd.
Lasse Reichstein Nielsen 2015/06/30 05:50:47 Done.
+ }
if (((x_used == 1) && (x_digits[0] == 1)) ||
((y_used == 1) && (y_digits[0] == 1))) return 1;
bool xy_cloned = false;
@@ -1754,7 +1759,9 @@ class _Bigint extends _IntegerImplementation implements int {
// No inverse if v != 1.
var i = m_used - 1;
while ((i > 0) && (v_digits[i] == 0)) --i;
- if ((i != 0) || (v_digits[0] != 1)) throw new RangeError("Not coprime");
+ if ((i != 0) || (v_digits[0] != 1)) {
+ throw new UnsupportedError("Not coprime");
+ }
if (d_neg) {
if ((d_digits[m_used] != 0) ||
@@ -1784,8 +1791,10 @@ class _Bigint extends _IntegerImplementation implements int {
// Returns 1/this % m, with m > 0.
int modInverse(int m) {
- if (m is! int) throw new ArgumentError(m);
- if (m <= 0) throw new RangeError(m);
+ if (m is! int) {
+ throw new ArgumentError.value(m, "modulus", "not an integer");
+ }
+ if (m <= 0) throw new RangeError.range(m, 1, null, "modulus");
if (m == 1) return 0;
m = m._toBigint();
var t = this;
@@ -1798,7 +1807,9 @@ class _Bigint extends _IntegerImplementation implements int {
// Returns gcd of abs(this) and abs(other), with this != 0 and other !=0.
int gcd(int other) {
- if (other is! int) throw new ArgumentError(other);
+ if (other is! int) {
+ throw new ArgumentError.value(other, "other", "not an integer");
+ }
return _binaryGcd(this, other._toBigint(), false);
}
}
« no previous file with comments | « no previous file | runtime/lib/integers.dart » ('j') | runtime/lib/integers.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698