Skip to content

Commit 359631a

Browse files
✨ feat(Integer): Implement i and n methods.
1 parent 91b1e02 commit 359631a

File tree

1 file changed

+93
-9
lines changed

1 file changed

+93
-9
lines changed

src/Integer.js

Lines changed: 93 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { DEFAULT_DISPLAY_BASE , ZeroDivisionError } from './' ;
22

33
import { ValueError } from '@aureooms/js-error' ;
44

5+
import { _from_number } from './_from_number' ;
6+
57
import {
68
stringify , convert , _trim_positive ,
79
_alloc , _copy , _zeros ,
@@ -81,6 +83,16 @@ export class Integer {
8183
return this.add(other).move(this);
8284
}
8385

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+
8496
sub ( other ) {
8597

8698
if ( this.is_negative !== other.is_negative ) {
@@ -136,6 +148,14 @@ export class Integer {
136148
return this.sub(other).move(this);
137149
}
138150

151+
subn ( number ) {
152+
return this.sub(_from_number(number));
153+
}
154+
155+
isubn ( number ) {
156+
return this.subn(number).move(this);
157+
}
158+
139159
mul ( other ) {
140160

141161
const result_is_negative = this.is_negative ^ other.is_negative ;
@@ -158,14 +178,22 @@ export class Integer {
158178
return this.mul(other).move(this);
159179
}
160180

181+
muln ( number ) {
182+
return this.mul(_from_number(number));
183+
}
184+
185+
imuln ( number ) {
186+
return this.muln(number).move(this);
187+
}
188+
161189
/**
162190
* Computes <code>this</code> raised to the <code>x</code>th power.
163191
* <code>x</code> is a double smaller or equal to 2^53.
164192
*
165193
* @param {Number} x The power to raise <code>this</code> to.
166194
* @return {Integer} <code>this ^ x</code>
167195
*/
168-
pow ( x ) {
196+
pown ( x ) {
169197

170198
const is_negative = this.is_negative & x & 1 ? -1 : 0 ;
171199

@@ -178,9 +206,18 @@ export class Integer {
178206

179207
}
180208

181-
ipow ( x ) {
209+
pow ( other ) {
210+
return this.pown( other.valueOf() ) ;
211+
}
212+
213+
ipow ( other ) {
182214
// 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);
184221
}
185222

186223
square ( ) {
@@ -197,20 +234,36 @@ export class Integer {
197234
return this.divmod( other )[0] ;
198235
}
199236

237+
divn ( number ) {
238+
return this.div(_from_number(number)) ;
239+
}
240+
200241
idiv ( other ) {
201242
// TODO optimize but be careful with side effects
202243
return this.div(other).move(this);
203244
}
204245

246+
idivn ( number ) {
247+
return this.divn(number).move(this);
248+
}
249+
205250
mod ( other ) {
206251
return this.divmod( other )[1] ;
207252
}
208253

254+
modn ( number ) {
255+
return this.mod(_from_number(number)) ;
256+
}
257+
209258
imod ( other ) {
210259
// TODO optimize but be careful with side effects
211260
return this.mod(other).move(this);
212261
}
213262

263+
imodn ( number ) {
264+
return this.modn(number).move(this);
265+
}
266+
214267
divmod ( other ) {
215268

216269
if ( other.iszero() ) throw new ZeroDivisionError( 'Integer division by zero' ) ; // optimize
@@ -268,6 +321,12 @@ export class Integer {
268321

269322
}
270323

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+
271330
opposite ( ) {
272331
return new Integer( this.base , ~this.is_negative , this.limbs ) ;
273332
}
@@ -326,8 +385,8 @@ export class Integer {
326385
return this.div( other ) ;
327386
}
328387

329-
cmp ( other ) {
330388

389+
cmp ( other ) {
331390
// TODO optimize with _trim_positive
332391

333392
if ( this.iszero( ) ) {
@@ -348,28 +407,58 @@ export class Integer {
348407

349408
}
350409

410+
cmpn ( number ) {
411+
return this.cmp(_from_number(number)) ;
351412
}
352413

353414
eq ( other ) {
354415
return this.cmp( other ) === 0 ;
355416
}
356417

418+
eqn ( number ) {
419+
return this.cmpn( number ) === 0 ;
420+
}
421+
357422
ge ( other ) {
358423
return this.cmp( other ) >= 0 ;
359424
}
360425

426+
gen ( number ) {
427+
return this.cmpn( number ) >= 0 ;
428+
}
429+
361430
gt ( other ) {
362431
return this.cmp( other ) > 0 ;
363432
}
364433

434+
gtn ( number ) {
435+
return this.cmpn( number ) > 0 ;
436+
}
437+
365438
le ( other ) {
366439
return this.cmp( other ) <= 0 ;
367440
}
368441

442+
len ( number ) {
443+
return this.cmpn( number ) <= 0 ;
444+
}
445+
369446
lt ( other ) {
370447
return this.cmp( other ) < 0 ;
371448
}
372449

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+
373462
gcd ( other ) {
374463
const r = this.base ;
375464
const a = this.limbs ;
@@ -404,11 +493,6 @@ export class Integer {
404493
} ;
405494
}
406495

407-
408-
ne ( other ) {
409-
return this.cmp( other ) !== 0 ;
410-
}
411-
412496
valueOf ( ) {
413497

414498
if (this.gtn(MAX_NUMBER)) throw new ValueError(`Cannot call valueOf on Integer larger than ${MAX_NUMBER}. Got ${this.toString()}`) ;

0 commit comments

Comments
 (0)