Skip to content

Commit 8799f1d

Browse files
🐛 fix(IntegerRing.from_number): Use less dubious implementation.
1 parent e81d088 commit 8799f1d

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/IntegerRing.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Integer } from './' ;
22
import { DEFAULT_DISPLAY_BASE } from './' ;
33
import { parse , convert } from '@aureooms/js-integer-big-endian' ;
44
import { TypeError , ValueError } from '@aureooms/js-error' ;
5+
import { _from_number } from './_from_number' ;
56

67
export class IntegerRing {
78

@@ -49,14 +50,11 @@ export class IntegerRing {
4950

5051
from_number ( number , is_negative = 0 ) {
5152

52-
if ( number < 0 ) {
53-
is_negative = ~is_negative ;
54-
number = -number ;
55-
}
53+
const dirty = _from_number(number) ;
5654

57-
const limbs = convert( 0x20000000000000 , this.base , [ number ] , 0 , 1 ) ;
55+
const limbs = dirty._limbs_in_base(this.base) ;
5856

59-
return new Integer( this.base , is_negative , limbs ) ;
57+
return new Integer( this.base , is_negative ^ dirty.is_negative , limbs ) ;
6058

6159
}
6260

test/src/ZZ/from.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'ava' ;
22

3-
import { ZZ , ValueError , TypeError } from '../../../src' ;
3+
import { ZZ , ValueError , TypeError , MIN_NUMBER , MAX_NUMBER } from '../../../src' ;
44

55
test( 'ZZ.from()' , t => {
66

@@ -20,6 +20,9 @@ test( 'ZZ.from()' , t => {
2020

2121
t.is( ZZ.from( [ 0 , 1 ] ).toString() , ZZ.base.toString() ) ;
2222

23+
t.is( MAX_NUMBER.toString() , ZZ.from(MAX_NUMBER).toString() ) ;
24+
t.is( MIN_NUMBER.toString() , ZZ.from(MIN_NUMBER).toString() ) ;
25+
2326
t.is( ZZ.from(ZZ.from(2, undefined, -1).pow(12)).toString(), '4096')
2427
t.is( ZZ.from(ZZ.from(2, undefined, 0).pow(12)).toString(), '4096')
2528
t.is( ZZ.from(ZZ.from(2, undefined, -1).pow(11)).toString(), '-2048')

0 commit comments

Comments
 (0)