@@ -2,6 +2,8 @@ import { DEFAULT_DISPLAY_BASE , ZeroDivisionError } from './' ;
2
2
3
3
import { ValueError } from '@aureooms/js-error' ;
4
4
5
+ import { _from_number } from './_from_number' ;
6
+
5
7
import {
6
8
stringify , convert , _trim_positive ,
7
9
_alloc , _copy , _zeros ,
@@ -81,6 +83,16 @@ export class Integer {
81
83
return this . add ( other ) . move ( this ) ;
82
84
}
83
85
86
+ addn ( number ) {
87
+ // TODO optimize
88
+ return this . add ( _from_number ( number ) ) ;
89
+ }
90
+
91
+ iaddn ( number ) {
92
+ // TODO optimize but be careful with side effects
93
+ return this . addn ( number ) . move ( this ) ;
94
+ }
95
+
84
96
sub ( other ) {
85
97
86
98
if ( this . is_negative !== other . is_negative ) {
@@ -136,6 +148,14 @@ export class Integer {
136
148
return this . sub ( other ) . move ( this ) ;
137
149
}
138
150
151
+ subn ( number ) {
152
+ return this . sub ( _from_number ( number ) ) ;
153
+ }
154
+
155
+ isubn ( number ) {
156
+ return this . subn ( number ) . move ( this ) ;
157
+ }
158
+
139
159
mul ( other ) {
140
160
141
161
const result_is_negative = this . is_negative ^ other . is_negative ;
@@ -158,14 +178,22 @@ export class Integer {
158
178
return this . mul ( other ) . move ( this ) ;
159
179
}
160
180
181
+ muln ( number ) {
182
+ return this . mul ( _from_number ( number ) ) ;
183
+ }
184
+
185
+ imuln ( number ) {
186
+ return this . muln ( number ) . move ( this ) ;
187
+ }
188
+
161
189
/**
162
190
* Computes <code>this</code> raised to the <code>x</code>th power.
163
191
* <code>x</code> is a double smaller or equal to 2^53.
164
192
*
165
193
* @param {Number } x The power to raise <code>this</code> to.
166
194
* @return {Integer } <code>this ^ x</code>
167
195
*/
168
- pow ( x ) {
196
+ pown ( x ) {
169
197
170
198
const is_negative = this . is_negative & x & 1 ? - 1 : 0 ;
171
199
@@ -178,9 +206,18 @@ export class Integer {
178
206
179
207
}
180
208
181
- ipow ( x ) {
209
+ pow ( other ) {
210
+ return this . pown ( other . valueOf ( ) ) ;
211
+ }
212
+
213
+ ipow ( other ) {
182
214
// TODO optimize but be careful with side effects
183
- return this . pow ( x ) . move ( this ) ;
215
+ return this . pow ( other ) . move ( this ) ;
216
+ }
217
+
218
+ ipown ( number ) {
219
+ // TODO optimize but be careful with side effects
220
+ return this . pown ( other ) . move ( this ) ;
184
221
}
185
222
186
223
square ( ) {
@@ -197,20 +234,36 @@ export class Integer {
197
234
return this . divmod ( other ) [ 0 ] ;
198
235
}
199
236
237
+ divn ( number ) {
238
+ return this . div ( _from_number ( number ) ) ;
239
+ }
240
+
200
241
idiv ( other ) {
201
242
// TODO optimize but be careful with side effects
202
243
return this . div ( other ) . move ( this ) ;
203
244
}
204
245
246
+ idivn ( number ) {
247
+ return this . divn ( number ) . move ( this ) ;
248
+ }
249
+
205
250
mod ( other ) {
206
251
return this . divmod ( other ) [ 1 ] ;
207
252
}
208
253
254
+ modn ( number ) {
255
+ return this . mod ( _from_number ( number ) ) ;
256
+ }
257
+
209
258
imod ( other ) {
210
259
// TODO optimize but be careful with side effects
211
260
return this . mod ( other ) . move ( this ) ;
212
261
}
213
262
263
+ imodn ( number ) {
264
+ return this . modn ( number ) . move ( this ) ;
265
+ }
266
+
214
267
divmod ( other ) {
215
268
216
269
if ( other . iszero ( ) ) throw new ZeroDivisionError ( 'Integer division by zero' ) ; // optimize
@@ -268,6 +321,12 @@ export class Integer {
268
321
269
322
}
270
323
324
+ idivmod ( other ) {
325
+ // TODO optimize but be careful with side effects
326
+ const [ q , r ] = this . divmod ( other ) ;
327
+ return [ q . move ( this ) , r ] ;
328
+ }
329
+
271
330
opposite ( ) {
272
331
return new Integer ( this . base , ~ this . is_negative , this . limbs ) ;
273
332
}
@@ -326,8 +385,8 @@ export class Integer {
326
385
return this . div ( other ) ;
327
386
}
328
387
329
- cmp ( other ) {
330
388
389
+ cmp ( other ) {
331
390
// TODO optimize with _trim_positive
332
391
333
392
if ( this . iszero ( ) ) {
@@ -348,28 +407,58 @@ export class Integer {
348
407
349
408
}
350
409
410
+ cmpn ( number ) {
411
+ return this . cmp ( _from_number ( number ) ) ;
351
412
}
352
413
353
414
eq ( other ) {
354
415
return this . cmp ( other ) === 0 ;
355
416
}
356
417
418
+ eqn ( number ) {
419
+ return this . cmpn ( number ) === 0 ;
420
+ }
421
+
357
422
ge ( other ) {
358
423
return this . cmp ( other ) >= 0 ;
359
424
}
360
425
426
+ gen ( number ) {
427
+ return this . cmpn ( number ) >= 0 ;
428
+ }
429
+
361
430
gt ( other ) {
362
431
return this . cmp ( other ) > 0 ;
363
432
}
364
433
434
+ gtn ( number ) {
435
+ return this . cmpn ( number ) > 0 ;
436
+ }
437
+
365
438
le ( other ) {
366
439
return this . cmp ( other ) <= 0 ;
367
440
}
368
441
442
+ len ( number ) {
443
+ return this . cmpn ( number ) <= 0 ;
444
+ }
445
+
369
446
lt ( other ) {
370
447
return this . cmp ( other ) < 0 ;
371
448
}
372
449
450
+ ltn ( number ) {
451
+ return this . cmpn ( number ) < 0 ;
452
+ }
453
+
454
+ ne ( other ) {
455
+ return this . cmp ( other ) !== 0 ;
456
+ }
457
+
458
+ nen ( number ) {
459
+ return this . cmpn ( number ) !== 0 ;
460
+ }
461
+
373
462
gcd ( other ) {
374
463
const r = this . base ;
375
464
const a = this . limbs ;
@@ -404,11 +493,6 @@ export class Integer {
404
493
} ;
405
494
}
406
495
407
-
408
- ne ( other ) {
409
- return this . cmp ( other ) !== 0 ;
410
- }
411
-
412
496
valueOf ( ) {
413
497
414
498
if ( this . gtn ( MAX_NUMBER ) ) throw new ValueError ( `Cannot call valueOf on Integer larger than ${ MAX_NUMBER } . Got ${ this . toString ( ) } ` ) ;
0 commit comments