Skip to content

Commit 624ced5

Browse files
✨ feat(Integer): Add sign testing convenience methods.
1 parent 69d0f32 commit 624ced5

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

src/Integer.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,10 +366,26 @@ export class Integer {
366366
return eq( this.limbs , 0 , this.limbs.length , [ 1 ] , 0 , 1 ) ;
367367
}
368368

369-
nonzero ( ) {
369+
isnonzero ( ) {
370370
return !this.iszero();
371371
}
372372

373+
isnegative ( ) {
374+
return this.is_negative ;
375+
}
376+
377+
ispositive ( ) {
378+
return this.sign() > 0 ;
379+
}
380+
381+
isnonnegative ( ) {
382+
return !this.negative();
383+
}
384+
385+
isnonpositive ( ) {
386+
return !this.positive() ;
387+
}
388+
373389
parity ( ) {
374390
// TODO optimize this, there is a much faster way to test for parity
375391
// when the base is a multiple of two

test/src/Integer/isnonzero.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import test from 'ava' ;
2+
import { ZZ } from '../../../src' ;
3+
4+
test( 'ZZ.$0().isnonzero()' , t => { t.is( ZZ.$0().isnonzero() , false ) ; } ) ;
5+
test( 'ZZ.$1().isnonzero()' , t => { t.is( ZZ.$1().isnonzero() , true ) ; } ) ;
6+
7+
test( 'ZZ.from(-2).isnonzero()' , t => { t.is( ZZ.from(-2).isnonzero() , true ) ; } ) ;
8+
test( 'ZZ.from(-1).isnonzero()' , t => { t.is( ZZ.from(-1).isnonzero() , true ) ; } ) ;
9+
test( 'ZZ.from(0).isnonzero()' , t => { t.is( ZZ.from(0).isnonzero() , false ) ; } ) ;
10+
test( 'ZZ.from(-0).isnonzero()' , t => { t.is( ZZ.from(-0).isnonzero() , false ) ; } ) ;
11+
test( 'ZZ.from(1).isnonzero()' , t => { t.is( ZZ.from(1).isnonzero() , true ) ; } ) ;
12+
test( 'ZZ.from(2).isnonzero()' , t => { t.is( ZZ.from(2).isnonzero() , true ) ; } ) ;
13+
14+
test( 'ZZ.from("-2").isnonzero()' , t => { t.is( ZZ.from("-2").isnonzero() , true ) ; } ) ;
15+
test( 'ZZ.from("-1").isnonzero()' , t => { t.is( ZZ.from("-1").isnonzero() , true ) ; } ) ;
16+
test( 'ZZ.from("0").isnonzero()' , t => { t.is( ZZ.from("0").isnonzero() , false ) ; } ) ;
17+
test( 'ZZ.from("-0").isnonzero()' , t => { t.is( ZZ.from("-0").isnonzero() , false ) ; } ) ;
18+
test( 'ZZ.from("1").isnonzero()' , t => { t.is( ZZ.from("1").isnonzero() , true ) ; } ) ;
19+
test( 'ZZ.from("2").isnonzero()' , t => { t.is( ZZ.from("2").isnonzero() , true ) ; } ) ;

test/src/Integer/nonzero.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)