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 part of dart.core; | 5 part of dart.core; |
6 | 6 |
7 /** | 7 /** |
8 * An arbitrarily large integer. | 8 * An arbitrarily large integer. |
9 * | 9 * |
10 * **Note:** When compiling to JavaScript, integers are | 10 * **Note:** When compiling to JavaScript, integers are |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 /** | 81 /** |
82 * Shift the bits of this integer to the left by [shiftAmount]. | 82 * Shift the bits of this integer to the left by [shiftAmount]. |
83 * | 83 * |
84 * Shifting to the left makes the number larger, effectively multiplying | 84 * Shifting to the left makes the number larger, effectively multiplying |
85 * the number by `pow(2, shiftIndex)`. | 85 * the number by `pow(2, shiftIndex)`. |
86 * | 86 * |
87 * There is no limit on the size of the result. It may be relevant to | 87 * There is no limit on the size of the result. It may be relevant to |
88 * limit intermediate values by using the "and" operator with a suitable | 88 * limit intermediate values by using the "and" operator with a suitable |
89 * mask. | 89 * mask. |
90 * | 90 * |
91 * It is an error of [shiftAmount] is negative. | 91 * It is an error if [shiftAmount] is negative. |
92 */ | 92 */ |
93 int operator <<(int shiftAmount); | 93 int operator <<(int shiftAmount); |
94 | 94 |
95 /** | 95 /** |
96 * Shift the bits of this integer to the right by [shiftAmount]. | 96 * Shift the bits of this integer to the right by [shiftAmount]. |
97 * | 97 * |
98 * Shifting to the right makes the number smaller and drops the least | 98 * Shifting to the right makes the number smaller and drops the least |
99 * significant bits, effectively doing an integer division by | 99 * significant bits, effectively doing an integer division by |
100 *`pow(2, shiftIndex)`. | 100 *`pow(2, shiftIndex)`. |
101 * | 101 * |
102 * It is an error of [shiftAmount] is negative. | 102 * It is an error if [shiftAmount] is negative. |
103 */ | 103 */ |
104 int operator >>(int shiftAmount); | 104 int operator >>(int shiftAmount); |
105 | 105 |
| 106 /** |
| 107 * Returns this integer to the power of [exponent] modulo [modulus]. |
| 108 * |
| 109 * The [exponent] must be non-negative and [modulus] must be |
| 110 * positive. |
| 111 */ |
| 112 int modPow(int exponent, int modulus); |
| 113 |
| 114 /** |
| 115 * Returns the modular multiplicative inverse of this integer |
| 116 * modulo [modulus]. |
| 117 * |
| 118 * The [modulus] must be positive. |
| 119 * |
| 120 * It is an error if no modular inverse exists. |
| 121 */ |
| 122 int modInverse(int modulus); |
| 123 |
| 124 /** |
| 125 * Returns the greatest common divisor of this integer and [other]. |
| 126 * |
| 127 * If either number is non-zero, the result is the numerically greatest |
| 128 * integer dividing both `this` and `other`. |
| 129 * |
| 130 * The greatest common divisor is independent of the order, |
| 131 * so `x.gcd(y)` is always the same as `y.gcd(x)`. |
| 132 * |
| 133 * For any integer `x`, `x.gcd(x)` is `x.abs()`. |
| 134 * |
| 135 * If both `this` and `other` is zero, the result is also zero. |
| 136 */ |
| 137 int gcd(int other); |
| 138 |
106 /** Returns true if and only if this integer is even. */ | 139 /** Returns true if and only if this integer is even. */ |
107 bool get isEven; | 140 bool get isEven; |
108 | 141 |
109 /** Returns true if and only if this integer is odd. */ | 142 /** Returns true if and only if this integer is odd. */ |
110 bool get isOdd; | 143 bool get isOdd; |
111 | 144 |
112 /** | 145 /** |
113 * Returns the minimum number of bits required to store this integer. | 146 * Returns the minimum number of bits required to store this integer. |
114 * | 147 * |
115 * The number of bits excludes the sign bit, which gives the natural length | 148 * The number of bits excludes the sign bit, which gives the natural length |
(...skipping 14 matching lines...) Expand all Loading... |
130 * (-3).bitLength == 2; // 11111101 | 163 * (-3).bitLength == 2; // 11111101 |
131 * (-4).bitLength == 2; // 11111100 | 164 * (-4).bitLength == 2; // 11111100 |
132 */ | 165 */ |
133 int get bitLength; | 166 int get bitLength; |
134 | 167 |
135 /** | 168 /** |
136 * Returns the least significant [width] bits of this integer as a | 169 * Returns the least significant [width] bits of this integer as a |
137 * non-negative number (i.e. unsigned representation). The returned value has | 170 * non-negative number (i.e. unsigned representation). The returned value has |
138 * zeros in all bit positions higher than [width]. | 171 * zeros in all bit positions higher than [width]. |
139 * | 172 * |
140 * (-1).toUnsigned(5) == 32 // 11111111 -> 00011111 | 173 * (-1).toUnsigned(5) == 31 // 11111111 -> 00011111 |
141 * | 174 * |
142 * This operation can be used to simulate arithmetic from low level languages. | 175 * This operation can be used to simulate arithmetic from low level languages. |
143 * For example, to increment an 8 bit quantity: | 176 * For example, to increment an 8 bit quantity: |
144 * | 177 * |
145 * q = (q + 1).toUnsigned(8); | 178 * q = (q + 1).toUnsigned(8); |
146 * | 179 * |
147 * `q` will count from `0` up to `255` and then wrap around to `0`. | 180 * `q` will count from `0` up to `255` and then wrap around to `0`. |
148 * | 181 * |
149 * If the input fits in [width] bits without truncation, the result is the | 182 * If the input fits in [width] bits without truncation, the result is the |
150 * same as the input. The minimum width needed to avoid truncation of `x` is | 183 * same as the input. The minimum width needed to avoid truncation of `x` is |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 * The returned string is parsable by [parse]. | 267 * The returned string is parsable by [parse]. |
235 * For any `int` [:i:], it is guaranteed that | 268 * For any `int` [:i:], it is guaranteed that |
236 * [:i == int.parse(i.toString()):]. | 269 * [:i == int.parse(i.toString()):]. |
237 */ | 270 */ |
238 String toString(); | 271 String toString(); |
239 | 272 |
240 /** | 273 /** |
241 * Converts [this] to a string representation in the given [radix]. | 274 * Converts [this] to a string representation in the given [radix]. |
242 * | 275 * |
243 * In the string representation, lower-case letters are used for digits above | 276 * In the string representation, lower-case letters are used for digits above |
244 * '9'. | 277 * '9', with 'a' being 10 an 'z' being 35. |
245 * | 278 * |
246 * The [radix] argument must be an integer in the range 2 to 36. | 279 * The [radix] argument must be an integer in the range 2 to 36. |
247 */ | 280 */ |
248 String toRadixString(int radix); | 281 String toRadixString(int radix); |
249 | 282 |
250 /** | 283 /** |
251 * Parse [source] as an integer literal and return its value. | 284 * Parse [source] as a, possibly signed, integer literal and return its value. |
252 * | |
253 * The [radix] must be in the range 2..36. The digits used are | |
254 * first the decimal digits 0..9, and then the letters 'a'..'z'. | |
255 * Accepts capital letters as well. | |
256 * | |
257 * If no [radix] is given then it defaults to 10, unless the string starts | |
258 * with "0x", "-0x" or "+0x", in which case the radix is set to 16 and the | |
259 * "0x" is ignored. | |
260 * | 285 * |
261 * The [source] must be a non-empty sequence of base-[radix] digits, | 286 * The [source] must be a non-empty sequence of base-[radix] digits, |
262 * optionally prefixed with a minus or plus sign ('-' or '+'). | 287 * optionally prefixed with a minus or plus sign ('-' or '+'). |
263 * | 288 * |
264 * It must always be the case for an int [:n:] and radix [:r:] that | 289 * The [radix] must be in the range 2..36. The digits used are |
| 290 * first the decimal digits 0..9, and then the letters 'a'..'z' with |
| 291 * values 10 through 35. Also accepts upper-case letters with the same |
| 292 * values as the lower-case ones. |
| 293 * |
| 294 * If no [radix] is given then it defaults to 10. In this case, the [source] |
| 295 * digits may also start with `0x`, in which case the number is interpreted |
| 296 * as a hexadecimal literal, which effectively means that the `0x` is ignored |
| 297 * and the radix is instead set to 16. |
| 298 * |
| 299 * For any int [:n:] and radix [:r:], it is guaranteed that |
265 * [:n == int.parse(n.toRadixString(r), radix: r):]. | 300 * [:n == int.parse(n.toRadixString(r), radix: r):]. |
266 * | 301 * |
267 * If the [source] is not a valid integer literal, optionally prefixed by a | 302 * If the [source] is not a valid integer literal, optionally prefixed by a |
268 * sign, the [onError] is called with the [source] as argument, and its return | 303 * sign, the [onError] is called with the [source] as argument, and its return |
269 * value is used instead. If no [onError] is provided, a [FormatException] | 304 * value is used instead. If no [onError] is provided, a [FormatException] |
270 * is thrown. | 305 * is thrown. |
271 * | 306 * |
| 307 * The [onError] handler can be chosen to return `null`. This is preferable |
| 308 * to to throwing and then immediately catching the [FormatException]. |
| 309 * Example: |
| 310 * |
| 311 * var value = int.parse(text, onError: (source) => null); |
| 312 * if (value == null) ... handle the problem |
| 313 * |
272 * The [onError] function is only invoked if [source] is a [String]. It is | 314 * The [onError] function is only invoked if [source] is a [String]. It is |
273 * not invoked if the [source] is, for example, `null`. | 315 * not invoked if the [source] is, for example, `null`. |
274 */ | 316 */ |
275 external static int parse(String source, | 317 external static int parse(String source, |
276 { int radix, | 318 { int radix, |
277 int onError(String source) }); | 319 int onError(String source) }); |
278 } | 320 } |
OLD | NEW |