Skip to content

Commit 505a4dd

Browse files
✨ feat(cmp): Add more efficient cmp.
Works when working with arbitrary precision integers.
1 parent 0c7a2f0 commit 505a4dd

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

src/cmp_no_bounds.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export function _cmp_no_bounds ( { jz , lt0 , mul , cmp } ) {
2+
return function (a,b,c,d) {
3+
if (jz(b)) {
4+
if(lt0(a)) {
5+
// a/b = -Infinity
6+
return jz(d) ? lt0(c) ?
7+
0 : // b/d = -Infinity
8+
-1 : // b/d = Infinity
9+
-1 ; // b/d is finite
10+
}
11+
else {
12+
// a/b = Infinity
13+
return jz(d) ? lt0(c) ?
14+
1 : // b/d = -Infinity
15+
0 : // b/d = Infinity
16+
1 ; // b/d is finite
17+
}
18+
}
19+
// a/b is finite
20+
if (jz(d)) return lt0(c) ?
21+
1 : // b/d = -Infinity
22+
-1 ; // b/d = Infinity
23+
24+
return cmp(mul(a,d), mul(b,c)) ;
25+
} ;
26+
}

src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './div' ;
33
export * from './mul' ;
44
export * from './sub' ;
55
export * from './cmp' ;
6+
export * from './cmp_no_bounds' ;

test/src/core.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import test from 'ava';
2-
import { _add , _sub , _mul , _div , _cmp } from '../../src';
2+
import { _add , _sub , _mul , _div , _cmp , _cmp_no_bounds } from '../../src';
33

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

77
const GOOGOL = '10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' ;
88

9-
function macro ( t , alu , [ [ _x , _y , _z , factory ] , a , b , c , d , e ] ) {
9+
function macro ( t , alu , [ [ _x , _y , factory ] , a , b , c , d , e ] ) {
1010

1111
const apply = factory( alu );
1212

@@ -26,7 +26,7 @@ function macro ( t , alu , [ [ _x , _y , _z , factory ] , a , b , c , d , e ] )
2626
}
2727

2828
macro.title = ( _ , alu , [ [ name , op , impl ] , a , b , c , d , e] ) => {
29-
return `${name}<${impl}, ${alu.name}> ${a}/${b} ${op} ${c}/${d} = ${e}` ;
29+
return `${name}<${impl.name}, ${alu.name}> ${a}/${b} ${op} ${c}/${d} = ${e}` ;
3030
} ;
3131

3232
const ALU = [
@@ -60,11 +60,11 @@ const ALU = [
6060
}
6161
];
6262

63-
const add = [ 'add' , '+' , '_add' , alu => _add( alu ) ] ;
64-
const sub = [ 'sub' , '-' , '_sub' , alu => _sub( alu ) ] ;
65-
const mul = [ 'mul' , '*' , '_mul' , alu => _mul( alu ) ] ;
66-
const div = [ 'div' , '/' , '_div' , alu => _div( alu ) ] ;
67-
const cmp = [ 'cmp' , '~' , '_cmp' , alu => _cmp( alu ) ] ;
63+
const add = [ 'add' , '+' , [ _add ] ] ;
64+
const sub = [ 'sub' , '-' , [ _sub ] ] ;
65+
const mul = [ 'mul' , '*' , [ _mul ] ] ;
66+
const div = [ 'div' , '/' , [ _div ] ] ;
67+
const cmp = [ 'cmp' , '~' , [ _cmp , _cmp_no_bounds ] ] ;
6868

6969
const PARAMS = [
7070

@@ -134,5 +134,6 @@ const PARAMS = [
134134
] ;
135135

136136
for (const alu of ALU)
137-
for (const params of PARAMS)
138-
test(macro, alu, params);
137+
for (const [[name, symbol, implementations], ...params] of PARAMS)
138+
for (const factory of implementations)
139+
test(macro, alu, [[name, symbol, factory], ...params]);

0 commit comments

Comments
 (0)