OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 part of dart.core; | |
6 | |
7 /** | |
8 * An arbitrarily large integer. | |
9 * | |
10 * **Note:** When compiling to JavaScript, integers are | |
11 * implemented as JavaScript numbers. When compiling to JavaScript, | |
12 * integers are therefore restricted to 53 significant bits because | |
13 * all JavaScript numbers are double-precision floating point | |
14 * values. The behavior of the operators and methods in the [int] | |
15 * class therefore sometimes differs between the Dart VM and Dart code | |
16 * compiled to JavaScript. | |
17 * | |
18 * It is a compile-time error for a class to attempt to extend or implement int. | |
19 */ | |
20 abstract class int extends num { | |
21 /** | |
22 * Returns the integer value of the given environment declaration [name]. | |
23 * | |
24 * The result is the same as would be returned by: | |
25 * | |
26 * int.parse(const String.fromEnvironment(name, defaultValue: ""), | |
27 * (_) => defaultValue) | |
28 * | |
29 * Example: | |
30 * | |
31 * const int.fromEnvironment("defaultPort", defaultValue: 80) | |
32 */ | |
33 external const factory int.fromEnvironment(String name, {int defaultValue}); | |
34 | |
35 /** | |
36 * Bit-wise and operator. | |
37 * | |
38 * Treating both `this` and [other] as sufficiently large two's component | |
39 * integers, the result is a number with only the bits set that are set in | |
40 * both `this` and [other] | |
41 * | |
42 * Of both operands are negative, the result is negative, otherwise | |
43 * the result is non-negative. | |
44 */ | |
45 int operator &(int other); | |
46 | |
47 /** | |
48 * Bit-wise or operator. | |
49 * | |
50 * Treating both `this` and [other] as sufficiently large two's component | |
51 * integers, the result is a number with the bits set that are set in either | |
52 * of `this` and [other] | |
53 * | |
54 * If both operands are non-negative, the result is non-negative, | |
55 * otherwise the result us negative. | |
56 */ | |
57 int operator |(int other); | |
58 | |
59 /** | |
60 * Bit-wise exclusive-or operator. | |
61 * | |
62 * Treating both `this` and [other] as sufficiently large two's component | |
63 * integers, the result is a number with the bits set that are set in one, | |
64 * but not both, of `this` and [other] | |
65 * | |
66 * If the operands have the same sign, the result is non-negative, | |
67 * otherwise the result is negative. | |
68 */ | |
69 int operator ^(int other); | |
70 | |
71 /** | |
72 * The bit-wise negate operator. | |
73 * | |
74 * Treating `this` as a sufficiently large two's component integer, | |
75 * the result is a number with the opposite bits set. | |
76 * | |
77 * This maps any integer `x` to `-x - 1`. | |
78 */ | |
79 int operator ~(); | |
80 | |
81 /** | |
82 * Shift the bits of this integer to the left by [shiftAmount]. | |
83 * | |
84 * Shifting to the left makes the number larger, effectively multiplying | |
85 * the number by `pow(2, shiftIndex)`. | |
86 * | |
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 | |
89 * mask. | |
90 * | |
91 * It is an error if [shiftAmount] is negative. | |
92 */ | |
93 int operator <<(int shiftAmount); | |
94 | |
95 /** | |
96 * Shift the bits of this integer to the right by [shiftAmount]. | |
97 * | |
98 * Shifting to the right makes the number smaller and drops the least | |
99 * significant bits, effectively doing an integer division by | |
100 *`pow(2, shiftIndex)`. | |
101 * | |
102 * It is an error if [shiftAmount] is negative. | |
103 */ | |
104 int operator >>(int shiftAmount); | |
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 | |
139 /** Returns true if and only if this integer is even. */ | |
140 bool get isEven; | |
141 | |
142 /** Returns true if and only if this integer is odd. */ | |
143 bool get isOdd; | |
144 | |
145 /** | |
146 * Returns the minimum number of bits required to store this integer. | |
147 * | |
148 * The number of bits excludes the sign bit, which gives the natural length | |
149 * for non-negative (unsigned) values. Negative values are complemented to | |
150 * return the bit position of the first bit that differs from the sign bit. | |
151 * | |
152 * To find the number of bits needed to store the value as a signed value, | |
153 * add one, i.e. use `x.bitLength + 1`. | |
154 * | |
155 * x.bitLength == (-x-1).bitLength | |
156 * | |
157 * 3.bitLength == 2; // 00000011 | |
158 * 2.bitLength == 2; // 00000010 | |
159 * 1.bitLength == 1; // 00000001 | |
160 * 0.bitLength == 0; // 00000000 | |
161 * (-1).bitLength == 0; // 11111111 | |
162 * (-2).bitLength == 1; // 11111110 | |
163 * (-3).bitLength == 2; // 11111101 | |
164 * (-4).bitLength == 2; // 11111100 | |
165 */ | |
166 int get bitLength; | |
167 | |
168 /** | |
169 * Returns the least significant [width] bits of this integer as a | |
170 * non-negative number (i.e. unsigned representation). The returned value has | |
171 * zeros in all bit positions higher than [width]. | |
172 * | |
173 * (-1).toUnsigned(5) == 31 // 11111111 -> 00011111 | |
174 * | |
175 * This operation can be used to simulate arithmetic from low level languages. | |
176 * For example, to increment an 8 bit quantity: | |
177 * | |
178 * q = (q + 1).toUnsigned(8); | |
179 * | |
180 * `q` will count from `0` up to `255` and then wrap around to `0`. | |
181 * | |
182 * If the input fits in [width] bits without truncation, the result is the | |
183 * same as the input. The minimum width needed to avoid truncation of `x` is | |
184 * given by `x.bitLength`, i.e. | |
185 * | |
186 * x == x.toUnsigned(x.bitLength); | |
187 */ | |
188 int toUnsigned(int width); | |
189 | |
190 /** | |
191 * Returns the least significant [width] bits of this integer, extending the | |
192 * highest retained bit to the sign. This is the same as truncating the value | |
193 * to fit in [width] bits using an signed 2-s complement representation. The | |
194 * returned value has the same bit value in all positions higher than [width]. | |
195 * | |
196 * V--sign bit-V | |
197 * 16.toSigned(5) == -16 // 00010000 -> 11110000 | |
198 * 239.toSigned(5) == 15 // 11101111 -> 00001111 | |
199 * ^ ^ | |
200 * | |
201 * This operation can be used to simulate arithmetic from low level languages. | |
202 * For example, to increment an 8 bit signed quantity: | |
203 * | |
204 * q = (q + 1).toSigned(8); | |
205 * | |
206 * `q` will count from `0` up to `127`, wrap to `-128` and count back up to | |
207 * `127`. | |
208 * | |
209 * If the input value fits in [width] bits without truncation, the result is | |
210 * the same as the input. The minimum width needed to avoid truncation of `x` | |
211 * is `x.bitLength + 1`, i.e. | |
212 * | |
213 * x == x.toSigned(x.bitLength + 1); | |
214 */ | |
215 int toSigned(int width); | |
216 | |
217 /** | |
218 * Return the negative value of this integer. | |
219 * | |
220 * The result of negating an integer always has the opposite sign, except | |
221 * for zero, which is its own negation. | |
222 */ | |
223 int operator -(); | |
224 | |
225 /** | |
226 * Returns the absolute value of this integer. | |
227 * | |
228 * For any integer `x`, the result is the same as `x < 0 ? -x : x`. | |
229 */ | |
230 int abs(); | |
231 | |
232 /** | |
233 * Returns the sign of this integer. | |
234 * | |
235 * Returns 0 for zero, -1 for values less than zero and | |
236 * +1 for values greater than zero. | |
237 */ | |
238 int get sign; | |
239 | |
240 /** Returns `this`. */ | |
241 int round(); | |
242 | |
243 /** Returns `this`. */ | |
244 int floor(); | |
245 | |
246 /** Returns `this`. */ | |
247 int ceil(); | |
248 | |
249 /** Returns `this`. */ | |
250 int truncate(); | |
251 | |
252 /** Returns `this.toDouble()`. */ | |
253 double roundToDouble(); | |
254 | |
255 /** Returns `this.toDouble()`. */ | |
256 double floorToDouble(); | |
257 | |
258 /** Returns `this.toDouble()`. */ | |
259 double ceilToDouble(); | |
260 | |
261 /** Returns `this.toDouble()`. */ | |
262 double truncateToDouble(); | |
263 | |
264 /** | |
265 * Returns a String-representation of this integer. | |
266 * | |
267 * The returned string is parsable by [parse]. | |
268 * For any `int` [:i:], it is guaranteed that | |
269 * [:i == int.parse(i.toString()):]. | |
270 */ | |
271 String toString(); | |
272 | |
273 /** | |
274 * Converts [this] to a string representation in the given [radix]. | |
275 * | |
276 * In the string representation, lower-case letters are used for digits above | |
277 * '9', with 'a' being 10 an 'z' being 35. | |
278 * | |
279 * The [radix] argument must be an integer in the range 2 to 36. | |
280 */ | |
281 String toRadixString(int radix); | |
282 | |
283 /** | |
284 * Parse [source] as a, possibly signed, integer literal and return its value. | |
285 * | |
286 * The [source] must be a non-empty sequence of base-[radix] digits, | |
287 * optionally prefixed with a minus or plus sign ('-' or '+'). | |
288 * | |
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 | |
300 * [:n == int.parse(n.toRadixString(r), radix: r):]. | |
301 * | |
302 * If the [source] is not a valid integer literal, optionally prefixed by a | |
303 * sign, the [onError] is called with the [source] as argument, and its return | |
304 * value is used instead. If no [onError] is provided, a [FormatException] | |
305 * is thrown. | |
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 * | |
314 * The [onError] function is only invoked if [source] is a [String]. It is | |
315 * not invoked if the [source] is, for example, `null`. | |
316 */ | |
317 external static int parse(String source, | |
318 { int radix, | |
319 int onError(String source) }); | |
320 } | |
OLD | NEW |