13
13
* @author Jesse Alama <[email protected] >
14
14
*/
15
15
16
+ import JSBI from "jsbi" ;
17
+
16
18
import {
17
19
RoundingMode ,
18
20
ROUNDING_MODES ,
@@ -28,7 +30,7 @@ const EXPONENT_MAX = 6111;
28
30
const NORMAL_EXPONENT_MAX = 6144 ;
29
31
const MAX_SIGNIFICANT_DIGITS = 34 ;
30
32
31
- const bigTen = BigInt ( 10 ) ;
33
+ const bigTen = JSBI . BigInt ( 10 ) ;
32
34
33
35
type NaNValue = "NaN" ;
34
36
type InfiniteValue = "Infinity" | "-Infinity" ;
@@ -39,7 +41,7 @@ type Decimal128Value = NaNValue | InfiniteValue | FiniteValue;
39
41
const NAN = "NaN" ;
40
42
const POSITIVE_INFINITY = "Infinity" ;
41
43
const NEGATIVE_INFINITY = "-Infinity" ;
42
- const TEN_MAX_EXPONENT = bigTen ** BigInt ( MAX_SIGNIFICANT_DIGITS ) ;
44
+ const TEN_MAX_EXPONENT = JSBI . exponentiate ( bigTen , JSBI . BigInt ( MAX_SIGNIFICANT_DIGITS ) ) ;
43
45
44
46
function pickQuantum (
45
47
d : "0" | "-0" | Rational ,
@@ -80,7 +82,7 @@ function adjustDecimal128(d: Decimal): Decimal128Value {
80
82
81
83
let coef = d . coefficient ( ) ;
82
84
83
- if ( coef <= TEN_MAX_EXPONENT && EXPONENT_MIN <= q && q <= EXPONENT_MAX ) {
85
+ if ( JSBI . LE ( coef , TEN_MAX_EXPONENT ) && JSBI . LE ( EXPONENT_MIN , q ) && JSBI . LE ( q , EXPONENT_MAX ) ) {
84
86
return d ;
85
87
}
86
88
@@ -166,10 +168,18 @@ export class Decimal128 {
166
168
private readonly _isFinite : boolean = true ;
167
169
private readonly _isNegative : boolean = false ;
168
170
169
- constructor ( n : string | number | bigint | Decimal ) {
171
+ constructor ( n : string | number | bigint | JSBI | Decimal ) {
170
172
let data ;
171
173
if ( "object" === typeof n ) {
172
- data = n ;
174
+ if ( n instanceof JSBI ) {
175
+ // Note: when using babel-plugin-transform-jsbi-to-bigint,
176
+ // the condition above may get transpiled into "typeof n === 'bigint'",
177
+ // causing the condition above to always be false.
178
+ // This is fine as we will be hanlding bigint below.
179
+ data = handleDecimalNotation ( n . toString ( ) )
180
+ } else {
181
+ data = n ;
182
+ }
173
183
} else {
174
184
let s : string ;
175
185
@@ -262,7 +272,7 @@ export class Decimal128 {
262
272
let decimalOne = new Decimal128 ( "1" ) ;
263
273
let decimalTen = new Decimal128 ( "10" ) ;
264
274
265
- while ( 0 <= x . cmp ( decimalTen ) ) {
275
+ while ( JSBI . LE ( 0 , x . cmp ( decimalTen ) ) ) {
266
276
x = x . scale10 ( - 1 ) ;
267
277
}
268
278
@@ -303,7 +313,7 @@ export class Decimal128 {
303
313
) ;
304
314
}
305
315
306
- private coefficient ( ) : bigint {
316
+ private coefficient ( ) : JSBI {
307
317
let d = this . d as Decimal ;
308
318
return d . coefficient ( ) ;
309
319
}
@@ -463,7 +473,7 @@ export class Decimal128 {
463
473
464
474
let n = opts . digits ;
465
475
466
- if ( n <= 0 ) {
476
+ if ( JSBI . LE ( n , 0 ) ) {
467
477
throw new RangeError ( "Argument must be positive" ) ;
468
478
}
469
479
@@ -484,15 +494,15 @@ export class Decimal128 {
484
494
let [ lhs , rhs ] = s . split ( / [ . ] / ) ;
485
495
let p = this . isNegative ( ) ? "-" : "" ;
486
496
487
- if ( n <= lhs . length ) {
497
+ if ( JSBI . LE ( n , lhs . length ) ) {
488
498
if ( lhs . length === n ) {
489
499
return p + lhs ;
490
500
}
491
501
492
502
return p + s . substring ( 0 , n ) + "e+" + `${ lhs . length - n + 1 } ` ;
493
503
}
494
504
495
- if ( n <= lhs . length + rhs . length ) {
505
+ if ( JSBI . LE ( n , lhs . length + rhs . length ) ) {
496
506
let rounded = this . round ( n - lhs . length ) ;
497
507
return rounded . emitDecimal ( ) ;
498
508
}
@@ -523,7 +533,7 @@ export class Decimal128 {
523
533
524
534
let n = opts . digits ;
525
535
526
- if ( n <= 0 ) {
536
+ if ( JSBI . LE ( n , 0 ) ) {
527
537
throw new RangeError ( "Argument must be positive" ) ;
528
538
}
529
539
@@ -539,7 +549,7 @@ export class Decimal128 {
539
549
540
550
let p = this . isNegative ( ) ? "-" : "" ;
541
551
542
- if ( rhs . length <= n ) {
552
+ if ( JSBI . LE ( rhs . length , n ) ) {
543
553
return p + lhs + "." + rhs + "0" . repeat ( n - rhs . length ) + "e" + exp ;
544
554
}
545
555
@@ -558,7 +568,7 @@ export class Decimal128 {
558
568
return ! ! rhs . match ( / ^ 0 + $ / ) ;
559
569
}
560
570
561
- toBigInt ( ) : bigint {
571
+ toBigInt ( ) : JSBI {
562
572
if ( this . isNaN ( ) ) {
563
573
throw new RangeError ( "NaN cannot be converted to a BigInt" ) ;
564
574
}
@@ -573,7 +583,7 @@ export class Decimal128 {
573
583
) ;
574
584
}
575
585
576
- return BigInt ( this . toString ( ) ) ;
586
+ return JSBI . BigInt ( this . toString ( ) ) ;
577
587
}
578
588
579
589
toNumber ( ) : number {
@@ -931,34 +941,34 @@ export class Decimal128 {
931
941
let dividendCoefficient = this . coefficient ( ) ;
932
942
let divisorCoefficient = x . coefficient ( ) ;
933
943
934
- if ( dividendCoefficient !== 0n ) {
935
- while ( dividendCoefficient < divisorCoefficient ) {
936
- dividendCoefficient = dividendCoefficient * 10n ;
944
+ if ( JSBI . notEqual ( dividendCoefficient , JSBI . BigInt ( 0 ) ) ) {
945
+ while ( JSBI . LT ( dividendCoefficient , divisorCoefficient ) ) {
946
+ dividendCoefficient = JSBI . multiply ( dividendCoefficient , JSBI . BigInt ( 10 ) ) ;
937
947
adjust ++ ;
938
948
}
939
949
}
940
950
941
- while ( dividendCoefficient > divisorCoefficient * 10n ) {
942
- divisorCoefficient = divisorCoefficient * 10n ;
951
+ while ( JSBI . GT ( dividendCoefficient , JSBI . multiply ( divisorCoefficient , JSBI . BigInt ( 10 ) ) ) ) {
952
+ divisorCoefficient = JSBI . multiply ( divisorCoefficient , JSBI . BigInt ( 10 ) ) ;
943
953
adjust -- ;
944
954
}
945
955
946
- let resultCoefficient = 0n ;
956
+ let resultCoefficient = JSBI . BigInt ( 0 ) ;
947
957
let done = false ;
948
958
949
959
while ( ! done ) {
950
- while ( divisorCoefficient <= dividendCoefficient ) {
951
- dividendCoefficient = dividendCoefficient - divisorCoefficient ;
952
- resultCoefficient ++ ;
960
+ while ( JSBI . LE ( divisorCoefficient , dividendCoefficient ) ) {
961
+ dividendCoefficient = JSBI . subtract ( dividendCoefficient , divisorCoefficient ) ;
962
+ resultCoefficient = JSBI . add ( resultCoefficient , JSBI . BigInt ( 1 ) ) ;
953
963
}
954
964
if (
955
- ( dividendCoefficient === 0n && adjust >= 0 ) ||
965
+ ( JSBI . equal ( dividendCoefficient , JSBI . BigInt ( 0 ) ) && adjust >= 0 ) ||
956
966
resultCoefficient . toString ( ) . length > MAX_SIGNIFICANT_DIGITS
957
967
) {
958
968
done = true ;
959
969
} else {
960
- resultCoefficient = resultCoefficient * 10n ;
961
- dividendCoefficient = dividendCoefficient * 10n ;
970
+ resultCoefficient = JSBI . multiply ( resultCoefficient , JSBI . BigInt ( 10 ) ) ;
971
+ dividendCoefficient = JSBI . multiply ( dividendCoefficient , JSBI . BigInt ( 10 ) ) ;
962
972
adjust ++ ;
963
973
}
964
974
}
@@ -1123,7 +1133,7 @@ export class Decimal128 {
1123
1133
return this . exponent ( ) ;
1124
1134
}
1125
1135
1126
- scaledSignificand ( ) : bigint {
1136
+ scaledSignificand ( ) : JSBI {
1127
1137
if ( this . isNaN ( ) ) {
1128
1138
throw new RangeError ( "NaN does not have a scaled significand" ) ;
1129
1139
}
@@ -1133,7 +1143,7 @@ export class Decimal128 {
1133
1143
}
1134
1144
1135
1145
if ( this . isZero ( ) ) {
1136
- return 0n ;
1146
+ return JSBI . BigInt ( 0 ) ;
1137
1147
}
1138
1148
1139
1149
let v = this . cohort ( ) as Rational ;
0 commit comments