Skip to content

Commit 4bbbb22

Browse files
✨ feat: Implement comparison method.
Fixes #1.
1 parent 78dd4d8 commit 4bbbb22

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

src/cmp.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export function _cmp ( { jz , lt0 , lt , neg , divmod } ) {
2+
3+
const compare_positive_fractions = function (a,b,c,d) {
4+
if (jz(b)) {
5+
if(jz(d)) return 0;
6+
return 1;
7+
}
8+
if (jz(d)) return -1;
9+
const [ q1 , r1 ] = divmod(a, b) ;
10+
const [ q2 , r2 ] = divmod(c, d) ;
11+
if (lt(q1,q2)) return -1;
12+
if (lt(q2,q1)) return 1;
13+
return compare_positive_fractions(d,r2,b,r1);
14+
}
15+
16+
const cmp = function (a,b,c,d) {
17+
if (lt0(b)) { b = neg(b); a = neg(a); }
18+
if (lt0(d)) { d = neg(d); c = neg(c); }
19+
if (lt0(a)) {
20+
if (lt0(c)) return compare_positive_fractions(neg(c),d,neg(a),b);
21+
return -1;
22+
}
23+
if (lt0(c)) return 1;
24+
return compare_positive_fractions(a,b,c,d);
25+
}
26+
27+
return cmp ;
28+
29+
}

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export * from './add' ;
22
export * from './div' ;
33
export * from './mul' ;
44
export * from './sub' ;
5+
export * from './cmp' ;

test/src/core.js

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import test from 'ava';
2-
import { _add , _sub , _mul , _div } from '../../src';
2+
import { _add , _sub , _mul , _div , _cmp } from '../../src';
33

44
import int from 'int' ;
55
import { ZZ } from '@aureooms/js-integer' ;
66

7+
const GOOGOL = '10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' ;
8+
79
function macro ( t , alu , [ [ _x , _y , _z , factory ] , a , b , c , d , e ] ) {
810

911
const apply = factory( alu );
@@ -18,7 +20,8 @@ function macro ( t , alu , [ [ _x , _y , _z , factory ] , a , b , c , d , e ] )
1820

1921
const z = apply( a0 , a1 , b0 , b1 ) ;
2022

21-
t.is(num(z), e);
23+
if ( Number.isInteger(z) ) t.is(e, z) ;
24+
else t.is(e, num(z));
2225

2326
}
2427

@@ -28,13 +31,18 @@ macro.title = ( _ , alu , [ [ name , op , impl ] , a , b , c , d , e] ) => {
2831

2932
const ALU = [
3033
{
31-
name : 'node-int based alu',
34+
name : 'int',
3235
add : (a, b) => a.add(b),
3336
sub : (a, b) => a.sub(b),
3437
mul : (a, b) => a.mul(b),
3538
div : (a, b) => a.div(b),
3639
reg : x => int(x),
37-
str : x => x.toString()
40+
str : x => x.toString(),
41+
jz : x => x.eq(0),
42+
lt0 : x => x.lt(0),
43+
lt : (a,b) => a.lt(b),
44+
neg : x => x.neg(),
45+
divmod : (a,b) => [a.div(b), a.mod(b)],
3846
},
3947
{
4048
name : '@aureooms/js-integer',
@@ -43,15 +51,20 @@ const ALU = [
4351
mul : (a, b) => a.mul(b),
4452
div : (a, b) => a.div(b),
4553
reg : x => ZZ.from(x),
46-
str : x => x.toString()
47-
54+
str : x => x.toString(),
55+
jz : x => x.iszero(),
56+
lt0 : x => x.sign() < 0,
57+
lt : (a,b) => a.lt(b),
58+
neg : x => x.opposite(),
59+
divmod : (a,b) => a.divmod(b),
4860
}
4961
];
5062

5163
const add = [ 'add' , '+' , '_add' , alu => _add( alu.mul , alu.add ) ] ;
5264
const sub = [ 'sub' , '-' , '_sub' , alu => _sub( alu.mul , alu.sub ) ] ;
5365
const mul = [ 'mul' , '*' , '_mul' , alu => _mul( alu.mul ) ] ;
5466
const div = [ 'div' , '/' , '_div' , alu => _div( alu.mul ) ] ;
67+
const cmp = [ 'cmp' , '~' , '_cmp' , alu => _cmp( alu ) ] ;
5568

5669
const PARAMS = [
5770

@@ -83,6 +96,41 @@ const PARAMS = [
8396
[ div , '1', '3', '1', '6', 2 ] ,
8497
[ div , '1', '3', '2', '6', 1 ] ,
8598

99+
[ cmp , '3', '4', '1', '4', 1 ] ,
100+
[ cmp , '1', '10', '2', '10', -1 ] ,
101+
[ cmp , '5', '10', '2', '10', 1 ] ,
102+
[ cmp , '18', '10', '2', '10', 1 ] ,
103+
[ cmp , '1', '3', '1', '6', 1 ] ,
104+
[ cmp , '1', '3', '2', '6', 0 ] ,
105+
[ cmp , '6', '7', '13', '14', -1 ] ,
106+
[ cmp , '7', '6', '14', '13', 1 ] ,
107+
108+
[ cmp , '1', '0', '6', '7', 1 ] ,
109+
[ cmp , '1', '0', '-6', '7', 1 ] ,
110+
[ cmp , '-1', '0', '6', '7', -1 ] ,
111+
[ cmp , '-1', '0', '-6', '7', -1 ] ,
112+
113+
[ cmp , '1', '0', '999999', '1', 1 ] ,
114+
[ cmp , '1', '0', '-999999', '1', 1 ] ,
115+
[ cmp , '-1', '0', '999999', '1', -1 ] ,
116+
[ cmp , '-1', '0', '-999999', '1', -1 ] ,
117+
118+
[ cmp , '1', '0', '1', '0', 0 ] ,
119+
[ cmp , '1', '0', '-1', '0', 1 ] ,
120+
[ cmp , '-1', '0', '1', '0', -1 ] ,
121+
[ cmp , '-1', '0', '-1', '0', 0 ] ,
122+
123+
[ cmp , '1', '0', '999999', '0', 0 ] ,
124+
[ cmp , '1', '0', '-999999', '0', 1 ] ,
125+
[ cmp , '-1', '0', '999999', '0', -1 ] ,
126+
[ cmp , '-1', '0', '-999999', '0', 0 ] ,
127+
128+
[ cmp , GOOGOL, '7', GOOGOL, '3', -1 ] ,
129+
[ cmp , GOOGOL, '3', GOOGOL, '7', 1 ] ,
130+
[ cmp , GOOGOL, '7', GOOGOL, '7', 0 ] ,
131+
[ cmp , GOOGOL, '3', GOOGOL, '3', 0 ] ,
132+
[ cmp , GOOGOL, GOOGOL, GOOGOL, GOOGOL, 0 ] ,
133+
86134
] ;
87135

88136
for (const alu of ALU)

0 commit comments

Comments
 (0)