Skip to content

Commit 6d283dc

Browse files
✨ feat(pow): Add exponentiation function.
1 parent 4bd9e44 commit 6d283dc

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from './add' ;
22
export * from './div' ;
33
export * from './mul' ;
44
export * from './sub' ;
5+
export * from './pow' ;
56
export * from './cmp' ;
67
export * from './cmp_no_bounds' ;
78
export * from './simplify' ;

src/pow.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function _pow ( { pown } ) {
2+
3+
return function ( a , b , n ) {
4+
5+
return [ pown(a,n) , pown(b,n) ] ;
6+
7+
} ;
8+
9+
}
10+

test/src/core.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava';
2-
import { _add , _sub , _mul , _div , _cmp , _cmp_no_bounds , _simplify } from '../../src';
2+
import { _add , _sub , _mul , _div , _pow , _cmp , _cmp_no_bounds , _simplify } from '../../src';
33

44
import int from 'int' ;
55
import BN from 'bn.js' ;
@@ -29,6 +29,26 @@ binary.title = ( _ , alu , [ [ name , op , impl ] , a , b , c , d , e] ) => {
2929
return `${name}<${impl.name}, ${alu.name}> ${a}/${b} ${op} ${c}/${d} = ${e}` ;
3030
} ;
3131

32+
function binary_n ( t , alu , [ [ _x , _y , factory ] , a , b , n , e ] ) {
33+
34+
const apply = factory( alu );
35+
36+
const num = x => Number(alu.str(x[0])) / Number(alu.str(x[1])) ;
37+
38+
const a0 = alu.reg(a);
39+
const a1 = alu.reg(b);
40+
41+
const z = apply( a0 , a1 , n ) ;
42+
43+
if ( Number.isInteger(z) ) t.is(e, z) ;
44+
else t.is(e, num(z));
45+
46+
}
47+
48+
binary_n.title = ( _ , alu , [ [ name , op , impl ] , a , b , n , e] ) => {
49+
return `${name}<${impl.name}, ${alu.name}> ${a}/${b} ${op} ${n} = ${e}` ;
50+
} ;
51+
3252
function unary ( t , alu , [ [ _x , _y , factory ] , a , b , e ] ) {
3353

3454
const apply = factory( alu );
@@ -63,6 +83,7 @@ const ALU = [
6383
neg : x => x.neg(),
6484
sgn : x => x.cmp(0),
6585
divmod : (a,b) => [a.div(b), a.mod(b)],
86+
pown : (x,n) => x.pow(n),
6687
},
6788
{
6889
name : 'bn.js',
@@ -85,6 +106,7 @@ const ALU = [
85106
const gcd = a.gcd(b) ;
86107
return { u: b.div(gcd), v: a.div(gcd) } ;
87108
} ,
109+
pown : (x,n) => x.pow(new BN(n)),
88110
},
89111
{
90112
name : '@aureooms/js-integer',
@@ -101,13 +123,15 @@ const ALU = [
101123
neg : x => x.opposite(),
102124
divmod : (a,b) => a.divmod(b),
103125
egcd : (a,b) => a.egcd(b),
126+
pown : (x,n) => x.pown(n),
104127
}
105128
];
106129

107130
const add = [ 'add' , '+' , [ _add ] , binary ] ;
108131
const sub = [ 'sub' , '-' , [ _sub ] , binary ] ;
109132
const mul = [ 'mul' , '*' , [ _mul ] , binary ] ;
110133
const div = [ 'div' , '/' , [ _div ] , binary ] ;
134+
const pow = [ 'pow' , '^' , [ _pow ] , binary_n ] ;
111135
const cmp = [ 'cmp' , '~' , [ _cmp , _cmp_no_bounds ] , binary ] ;
112136
const simplify = [ 'simplify' , '=' , [ _simplify ] , unary , alu => alu.egcd ] ;
113137

@@ -141,6 +165,12 @@ const PARAMS = [
141165
[ div , '1', '3', '1', '6', 2 ] ,
142166
[ div , '1', '3', '2', '6', 1 ] ,
143167

168+
[ pow , '2', '3', 4, 16 / 81 ] ,
169+
[ pow , '-2', '3', 4, 16 / 81 ] ,
170+
171+
[ pow , '2', '3', 3, 8 / 27 ] ,
172+
[ pow , '-2', '3', 3, - 8 / 27 ] ,
173+
144174
[ cmp , '3', '4', '1', '4', 1 ] ,
145175
[ cmp , '1', '10', '2', '10', -1 ] ,
146176
[ cmp , '5', '10', '2', '10', 1 ] ,

0 commit comments

Comments
 (0)