1
1
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' ;
3
3
4
4
import int from 'int' ;
5
5
import BN from 'bn.js' ;
@@ -29,6 +29,26 @@ binary.title = ( _ , alu , [ [ name , op , impl ] , a , b , c , d , e] ) => {
29
29
return `${ name } <${ impl . name } , ${ alu . name } > ${ a } /${ b } ${ op } ${ c } /${ d } = ${ e } ` ;
30
30
} ;
31
31
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
+
32
52
function unary ( t , alu , [ [ _x , _y , factory ] , a , b , e ] ) {
33
53
34
54
const apply = factory ( alu ) ;
@@ -63,6 +83,7 @@ const ALU = [
63
83
neg : x => x . neg ( ) ,
64
84
sgn : x => x . cmp ( 0 ) ,
65
85
divmod : ( a , b ) => [ a . div ( b ) , a . mod ( b ) ] ,
86
+ pown : ( x , n ) => x . pow ( n ) ,
66
87
} ,
67
88
{
68
89
name : 'bn.js' ,
@@ -85,6 +106,7 @@ const ALU = [
85
106
const gcd = a . gcd ( b ) ;
86
107
return { u : b . div ( gcd ) , v : a . div ( gcd ) } ;
87
108
} ,
109
+ pown : ( x , n ) => x . pow ( new BN ( n ) ) ,
88
110
} ,
89
111
{
90
112
name : '@aureooms/js-integer' ,
@@ -101,13 +123,15 @@ const ALU = [
101
123
neg : x => x . opposite ( ) ,
102
124
divmod : ( a , b ) => a . divmod ( b ) ,
103
125
egcd : ( a , b ) => a . egcd ( b ) ,
126
+ pown : ( x , n ) => x . pown ( n ) ,
104
127
}
105
128
] ;
106
129
107
130
const add = [ 'add' , '+' , [ _add ] , binary ] ;
108
131
const sub = [ 'sub' , '-' , [ _sub ] , binary ] ;
109
132
const mul = [ 'mul' , '*' , [ _mul ] , binary ] ;
110
133
const div = [ 'div' , '/' , [ _div ] , binary ] ;
134
+ const pow = [ 'pow' , '^' , [ _pow ] , binary_n ] ;
111
135
const cmp = [ 'cmp' , '~' , [ _cmp , _cmp_no_bounds ] , binary ] ;
112
136
const simplify = [ 'simplify' , '=' , [ _simplify ] , unary , alu => alu . egcd ] ;
113
137
@@ -141,6 +165,12 @@ const PARAMS = [
141
165
[ div , '1' , '3' , '1' , '6' , 2 ] ,
142
166
[ div , '1' , '3' , '2' , '6' , 1 ] ,
143
167
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
+
144
174
[ cmp , '3' , '4' , '1' , '4' , 1 ] ,
145
175
[ cmp , '1' , '10' , '2' , '10' , - 1 ] ,
146
176
[ cmp , '5' , '10' , '2' , '10' , 1 ] ,
0 commit comments