@@ -871,7 +871,7 @@ const DEFAULT_CONSTRUCTOR_OPTIONS: FullySpecifiedConstructorOptions = {
871
871
} ;
872
872
873
873
type ToStringFormat = "decimal" | "exponential" ;
874
- const TO_STRING_FORMATS : string [ ] = [ "decimal" , "exponential" ] ;
874
+ const TOSTRING_FORMATS : string [ ] = [ "decimal" , "exponential" ] ;
875
875
876
876
interface ToStringOptions {
877
877
format ?: ToStringFormat ;
@@ -883,15 +883,15 @@ interface FullySpecifiedToStringOptions {
883
883
numDecimalDigits : number | undefined ;
884
884
}
885
885
886
- const DEFAULT_TO_STRING_OPTIONS : FullySpecifiedToStringOptions = {
886
+ const DEFAULT_TOSTRING_OPTIONS : FullySpecifiedToStringOptions = {
887
887
format : "decimal" ,
888
888
numDecimalDigits : undefined ,
889
889
} ;
890
890
891
891
function ensureFullySpecifiedConstructorOptions (
892
892
options ?: ConstructorOptions
893
893
) : FullySpecifiedConstructorOptions {
894
- let opts = DEFAULT_CONSTRUCTOR_OPTIONS ;
894
+ let opts = { ... DEFAULT_CONSTRUCTOR_OPTIONS } ;
895
895
896
896
if ( undefined === options ) {
897
897
return opts ;
@@ -904,9 +904,7 @@ function ensureFullySpecifiedConstructorOptions(
904
904
opts . roundingMode = options . roundingMode ;
905
905
}
906
906
907
- if ( undefined === options . normalize ) {
908
- opts . normalize = CONSTRUCTOR_SHOULD_NORMALIZE ;
909
- } else {
907
+ if ( "boolean" === typeof options . normalize ) {
910
908
opts . normalize = options . normalize ;
911
909
}
912
910
@@ -916,18 +914,15 @@ function ensureFullySpecifiedConstructorOptions(
916
914
function ensureFullySpecifiedToStringOptions (
917
915
options ?: ToStringOptions
918
916
) : FullySpecifiedToStringOptions {
919
- let opts : FullySpecifiedToStringOptions = {
920
- format : "decimal" ,
921
- numDecimalDigits : undefined ,
922
- } ;
917
+ let opts : FullySpecifiedToStringOptions = { ...DEFAULT_TOSTRING_OPTIONS } ;
923
918
924
919
if ( undefined === options ) {
925
920
return opts ;
926
921
}
927
922
928
923
if (
929
924
"string" === typeof options . format &&
930
- TO_STRING_FORMATS . includes ( options . format )
925
+ TOSTRING_FORMATS . includes ( options . format )
931
926
) {
932
927
opts . format = options . format ;
933
928
}
@@ -1055,7 +1050,7 @@ export class Decimal128 {
1055
1050
function emitDecimal ( ) : string {
1056
1051
if ( exp >= 0 ) {
1057
1052
return ensureDecimalDigits (
1058
- prefix + ( sg + "0" . repeat ( exp ) ) . replace ( / ^ 0 + $ / , "0" ) ,
1053
+ prefix + sg + "0" . repeat ( exp ) ,
1059
1054
options . numDecimalDigits
1060
1055
) ;
1061
1056
}
@@ -1108,13 +1103,6 @@ export class Decimal128 {
1108
1103
return emitDecimal ( ) ;
1109
1104
}
1110
1105
1111
- /**
1112
- * Is this Decimal128 actually an integer? That is: is there nothing after the decimal point?
1113
- */
1114
- private isInteger ( ) : boolean {
1115
- return ! ! this . toString ( ) . match ( / ^ - ? [ 0 - 9 ] + ( [ . ] 0 + ) ? $ / ) ;
1116
- }
1117
-
1118
1106
/**
1119
1107
* Return the absolute value of this Decimal128 value.
1120
1108
*
@@ -1168,9 +1156,8 @@ export class Decimal128 {
1168
1156
* Add this Decimal128 value to one or more Decimal128 values.
1169
1157
*
1170
1158
* @param x
1171
- * @param options
1172
1159
*/
1173
- add ( x : Decimal128 , options ?: ConstructorOptions ) : Decimal128 {
1160
+ add ( x : Decimal128 ) : Decimal128 {
1174
1161
if ( this . isNaN ( ) || x . isNaN ( ) ) {
1175
1162
return new Decimal128 ( NAN ) ;
1176
1163
}
@@ -1187,33 +1174,31 @@ export class Decimal128 {
1187
1174
return this . clone ( ) ;
1188
1175
}
1189
1176
1190
- if ( ! this . isFinite ( ) ) {
1191
- return this . clone ( ) ;
1192
- }
1193
-
1194
1177
if ( ! x . isFinite ( ) ) {
1195
1178
return x . clone ( ) ;
1196
1179
}
1197
1180
1198
1181
if ( this . isNegative && x . isNegative ) {
1199
- return this . negate ( ) . add ( x . negate ( ) , options ) . negate ( ) ;
1182
+ return this . negate ( ) . add ( x . negate ( ) ) . negate ( ) ;
1200
1183
}
1201
1184
1202
1185
let resultRat = Rational . add ( this . rat , x . rat ) ;
1203
1186
let initialResult = new Decimal128 (
1204
- resultRat . toDecimalPlaces ( MAX_SIGNIFICANT_DIGITS + 1 ) ,
1205
- options
1187
+ resultRat . toDecimalPlaces ( MAX_SIGNIFICANT_DIGITS + 1 )
1188
+ ) ;
1189
+ let adjusted = initialResult . setExponent (
1190
+ Math . min ( this . exponent , x . exponent )
1206
1191
) ;
1207
- return initialResult . setExponent ( Math . min ( this . exponent , x . exponent ) ) ;
1192
+
1193
+ return new Decimal128 ( adjusted . toString ( ) , { normalize : false } ) ;
1208
1194
}
1209
1195
1210
1196
/**
1211
1197
* Subtract another Decimal128 value from one or more Decimal128 values.
1212
1198
*
1213
1199
* @param x
1214
- * @param options
1215
1200
*/
1216
- subtract ( x : Decimal128 , options ?: ConstructorOptions ) : Decimal128 {
1201
+ subtract ( x : Decimal128 ) : Decimal128 {
1217
1202
if ( this . isNaN ( ) || x . isNaN ( ) ) {
1218
1203
return new Decimal128 ( NAN ) ;
1219
1204
}
@@ -1235,7 +1220,7 @@ export class Decimal128 {
1235
1220
}
1236
1221
1237
1222
if ( x . isNegative ) {
1238
- return this . add ( x . negate ( ) , options ) ;
1223
+ return this . add ( x . negate ( ) ) ;
1239
1224
}
1240
1225
1241
1226
let rendered = Rational . subtract ( this . rat , x . rat ) . toDecimalPlaces (
@@ -1246,7 +1231,7 @@ export class Decimal128 {
1246
1231
let adjusted = initialResult . setExponent (
1247
1232
Math . min ( this . exponent , x . exponent )
1248
1233
) ;
1249
- return new Decimal128 ( adjusted . toString ( ) , options ) ;
1234
+ return new Decimal128 ( adjusted . toString ( ) , { normalize : false } ) ;
1250
1235
}
1251
1236
1252
1237
/**
@@ -1255,9 +1240,8 @@ export class Decimal128 {
1255
1240
* If no arguments are given, return this value.
1256
1241
*
1257
1242
* @param x
1258
- * @param options
1259
1243
*/
1260
- multiply ( x : Decimal128 , options ?: ConstructorOptions ) : Decimal128 {
1244
+ multiply ( x : Decimal128 ) : Decimal128 {
1261
1245
if ( this . isNaN ( ) || x . isNaN ( ) ) {
1262
1246
return new Decimal128 ( NAN ) ;
1263
1247
}
@@ -1287,19 +1271,20 @@ export class Decimal128 {
1287
1271
}
1288
1272
1289
1273
if ( this . isNegative ) {
1290
- return this . negate ( ) . multiply ( x , options ) . negate ( ) ;
1274
+ return this . negate ( ) . multiply ( x ) . negate ( ) ;
1291
1275
}
1292
1276
1293
1277
if ( x . isNegative ) {
1294
- return this . multiply ( x . negate ( ) , options ) . negate ( ) ;
1278
+ return this . multiply ( x . negate ( ) ) . negate ( ) ;
1295
1279
}
1296
1280
1297
1281
let resultRat = Rational . multiply ( this . rat , x . rat ) ;
1298
1282
let initialResult = new Decimal128 (
1299
- resultRat . toDecimalPlaces ( MAX_SIGNIFICANT_DIGITS + 1 ) ,
1300
- options
1283
+ resultRat . toDecimalPlaces ( MAX_SIGNIFICANT_DIGITS + 1 )
1301
1284
) ;
1302
- return initialResult . setExponent ( this . exponent + x . exponent ) ;
1285
+ let adjusted = initialResult . setExponent ( this . exponent + x . exponent ) ;
1286
+
1287
+ return new Decimal128 ( adjusted . toString ( ) , { normalize : false } ) ;
1303
1288
}
1304
1289
1305
1290
private isZero ( ) : boolean {
@@ -1319,7 +1304,7 @@ export class Decimal128 {
1319
1304
*
1320
1305
* @param x
1321
1306
*/
1322
- divide ( x : Decimal128 , options ?: ConstructorOptions ) : Decimal128 {
1307
+ divide ( x : Decimal128 ) : Decimal128 {
1323
1308
if ( this . isNaN ( ) || x . isNaN ( ) ) {
1324
1309
return new Decimal128 ( NAN ) ;
1325
1310
}
@@ -1353,11 +1338,11 @@ export class Decimal128 {
1353
1338
}
1354
1339
1355
1340
if ( this . isNegative ) {
1356
- return this . negate ( ) . divide ( x , options ) . negate ( ) ;
1341
+ return this . negate ( ) . divide ( x ) . negate ( ) ;
1357
1342
}
1358
1343
1359
1344
if ( x . isNegative ) {
1360
- return this . divide ( x . negate ( ) , options ) . negate ( ) ;
1345
+ return this . divide ( x . negate ( ) ) . negate ( ) ;
1361
1346
}
1362
1347
1363
1348
let adjust = 0 ;
@@ -1402,10 +1387,7 @@ export class Decimal128 {
1402
1387
}
1403
1388
1404
1389
let resultExponent = this . exponent - ( x . exponent + adjust ) ;
1405
- return new Decimal128 (
1406
- `${ resultCoefficient } E${ resultExponent } ` ,
1407
- options
1408
- ) ;
1390
+ return new Decimal128 ( `${ resultCoefficient } E${ resultExponent } ` ) ;
1409
1391
}
1410
1392
1411
1393
/**
@@ -1473,30 +1455,29 @@ export class Decimal128 {
1473
1455
let s = this . toString ( ) ;
1474
1456
1475
1457
if ( s . match ( / ^ - / ) ) {
1476
- return new Decimal128 ( s . substring ( 1 ) ) ;
1458
+ return new Decimal128 ( s . substring ( 1 ) , { normalize : false } ) ;
1477
1459
}
1478
1460
1479
- return new Decimal128 ( "-" + s ) ;
1461
+ return new Decimal128 ( "-" + s , { normalize : false } ) ;
1480
1462
}
1481
1463
1482
1464
/**
1483
1465
* Return the remainder of this Decimal128 value divided by another Decimal128 value.
1484
1466
*
1485
1467
* @param d
1486
- * @param options
1487
1468
* @throws RangeError If argument is zero
1488
1469
*/
1489
- remainder ( d : Decimal128 , options ?: ConstructorOptions ) : Decimal128 {
1470
+ remainder ( d : Decimal128 ) : Decimal128 {
1490
1471
if ( this . isNaN ( ) || d . isNaN ( ) ) {
1491
1472
return new Decimal128 ( NAN ) ;
1492
1473
}
1493
1474
1494
1475
if ( this . isNegative ) {
1495
- return this . negate ( ) . remainder ( d , options ) . negate ( ) ;
1476
+ return this . negate ( ) . remainder ( d ) . negate ( ) ;
1496
1477
}
1497
1478
1498
1479
if ( d . isNegative ) {
1499
- return this . remainder ( d . negate ( ) , options ) ;
1480
+ return this . remainder ( d . negate ( ) ) ;
1500
1481
}
1501
1482
1502
1483
if ( ! this . isFinite ( ) ) {
@@ -1512,7 +1493,7 @@ export class Decimal128 {
1512
1493
}
1513
1494
1514
1495
let q = this . divide ( d ) . round ( 0 , ROUNDING_MODE_TRUNCATE ) ;
1515
- return this . subtract ( d . multiply ( q ) , options ) . abs ( ) ;
1496
+ return this . subtract ( d . multiply ( q ) ) . abs ( ) ;
1516
1497
}
1517
1498
1518
1499
normalize ( ) : Decimal128 {
@@ -1527,7 +1508,9 @@ export class Decimal128 {
1527
1508
let newExp = exp - 1 ;
1528
1509
let newSig = sig + "0" ;
1529
1510
1530
- return new Decimal128 ( `${ prefix } ${ newSig } E${ newExp } ` ) ;
1511
+ return new Decimal128 ( `${ prefix } ${ newSig } E${ newExp } ` , {
1512
+ normalize : false ,
1513
+ } ) ;
1531
1514
}
1532
1515
1533
1516
private setExponent ( newExp : number ) : Decimal128 {
0 commit comments