Skip to content

Commit d1b6eb4

Browse files
🐛 fix(cmp): Correct output when both numbers are negative.
1 parent 77791cb commit d1b6eb4

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/Integer.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,11 @@ export class Integer {
337337
const a = this.limbs ;
338338
const b = other._limbs_in_base( this.base ) ;
339339

340-
return cmp( a , 0 , a.length , b , 0 , b.length ) ;
340+
return this.is_negative === 0 ?
341+
cmp( a , 0 , a.length , b , 0 , b.length ) :
342+
cmp( b , 0 , b.length , a , 0 , a.length ) ;
343+
344+
}
341345

342346
}
343347

test/src/Integer/compare.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { ZZ } from '../../../src' ;
44

55
test( 'compare' , t => {
66

7-
t.is( ZZ.from(-1).cmp(ZZ.from(1)) , -1 ) ;
7+
t.is( ZZ.from(-1).cmpn(1) , -1 ) ;
8+
t.true( ZZ.from(-1).ltn(1) ) ;
9+
t.true( ZZ.from(-1).gtn(-2) ) ;
10+
t.true( ZZ.from(-1).len(1) ) ;
11+
t.true( ZZ.from(-1).len(-1) ) ;
12+
t.true( ZZ.from(7).gen(3) ) ;
13+
t.true( ZZ.from(13).eqn(13) ) ;
814

915
} ) ;

0 commit comments

Comments
 (0)