11import 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
44import int from 'int' ;
55import 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+
3252function 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
107130const add = [ 'add' , '+' , [ _add ] , binary ] ;
108131const sub = [ 'sub' , '-' , [ _sub ] , binary ] ;
109132const mul = [ 'mul' , '*' , [ _mul ] , binary ] ;
110133const div = [ 'div' , '/' , [ _div ] , binary ] ;
134+ const pow = [ 'pow' , '^' , [ _pow ] , binary_n ] ;
111135const cmp = [ 'cmp' , '~' , [ _cmp , _cmp_no_bounds ] , binary ] ;
112136const 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