Skip to content

Commit d47205c

Browse files
fix issue in pow and better coverage
1 parent f8cef57 commit d47205c

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

src/Integer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export class Integer {
162162
*/
163163
pow ( x ) {
164164

165-
const is_negative = Math.pow( this.is_negative , x ) ;
165+
const is_negative = this.is_negative & x & 1 ? -1 : 0 ;
166166

167167
const a = this.limbs ;
168168
const c = _zeros( Math.max( 1 , a.length * x ) ) ;

src/IntegerRing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class IntegerRing {
6262

6363
from_string ( string , base = 10 , is_negative = 0 ) {
6464

65-
if ( string.length === 0 ) throw 'IntegerRing#from_string cannot parse empty string.' ;
65+
if ( string.length === 0 ) throw new ValueError( 'IntegerRing#from_string cannot parse empty string.' ) ;
6666

6767
if ( string[0] === '-' ) return this.from_string( string.slice(1) , base , ~is_negative ) ;
6868

test/src/ZZ/from.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,19 @@ test( 'ZZ.from()' , t => {
1313
t.is( ZZ.from(false).toString() , '0' ) ;
1414
t.is( ZZ.from(1===0).toString() , '0' ) ;
1515
t.is( ZZ.from('+10').toString() , '10' ) ;
16+
t.is( ZZ.from(ZZ.from(2, undefined, -1).pow(12)).toString(), '4096')
17+
t.is( ZZ.from(ZZ.from(2, undefined, 0).pow(12)).toString(), '4096')
18+
t.is( ZZ.from(ZZ.from(2, undefined, -1).pow(11)).toString(), '-2048')
19+
t.is( ZZ.from(ZZ.from(2, undefined, 0).pow(11)).toString(), '2048')
1620

1721
t.throws( () => ZZ.from({}) , TypeError ) ;
1822
t.throws( () => ZZ.from(new Date()) , TypeError ) ;
1923
t.throws( () => ZZ.from(new Regex()) , TypeError ) ;
2024

25+
t.throws( () => ZZ.from('') , ValueError ) ;
26+
t.throws( () => ZZ.from('-') , ValueError ) ;
27+
t.throws( () => ZZ.from('+') , ValueError ) ;
28+
t.throws( () => ZZ.from('+---+-+') , ValueError ) ;
2129
t.throws( () => ZZ.from(1, 2) , ValueError ) ;
2230
t.throws( () => ZZ.from(true, 2) , ValueError ) ;
2331
t.throws( () => ZZ.from(ZZ.from(17), 2) , ValueError ) ;

0 commit comments

Comments
 (0)