@@ -224,22 +224,13 @@ export class Decimal128 {
224
224
}
225
225
226
226
private cohort ( ) : "0" | "-0" | Rational {
227
- let d = this . d ;
228
- if ( d instanceof Decimal ) {
229
- return d . cohort ;
230
- }
231
-
232
- throw new TypeError ( "Cannot compute cohort of a non-finite number" ) ;
227
+ let d = this . d as Decimal ;
228
+ return d . cohort ;
233
229
}
234
230
235
231
private quantum ( ) : number {
236
- let d = this . d ;
237
-
238
- if ( d instanceof Decimal ) {
239
- return d . quantum ;
240
- }
241
-
242
- throw new TypeError ( "Cannot compute quantum of a non-finite number" ) ;
232
+ let d = this . d as Decimal ;
233
+ return d . quantum ;
243
234
}
244
235
245
236
private isZero ( ) : boolean {
@@ -256,38 +247,6 @@ export class Decimal128 {
256
247
return v === "0" || v === "-0" ;
257
248
}
258
249
259
- private significandAndExponent ( ) : [ Rational , number ] {
260
- if ( ! this . isFinite ( ) ) {
261
- throw new RangeError ( "Infinity does not have a significand" ) ;
262
- }
263
-
264
- if ( this . isZero ( ) ) {
265
- throw new RangeError ( "Zero does not have a significand" ) ;
266
- }
267
-
268
- if ( this . isNegative ( ) ) {
269
- let [ s , e ] = this . negate ( ) . significandAndExponent ( ) ;
270
- return [ s . negate ( ) , e ] ;
271
- }
272
-
273
- let v = this . cohort ( ) as Rational ;
274
- let q = this . quantum ( ) as number ;
275
- let s = v ;
276
- let e = q ;
277
-
278
- while ( s . cmp ( ratOne ) < 0 ) {
279
- s = s . scale10 ( 1 ) ;
280
- e ++ ;
281
- }
282
-
283
- while ( s . cmp ( ratTen ) >= 0 ) {
284
- s = s . scale10 ( - 1 ) ;
285
- e -- ;
286
- }
287
-
288
- return [ s , e ] ;
289
- }
290
-
291
250
public exponent ( ) : number {
292
251
let mantissa = this . mantissa ( ) ;
293
252
let mantissaQuantum = mantissa . quantum ( ) ;
@@ -336,7 +295,12 @@ export class Decimal128 {
336
295
return this . clone ( ) ;
337
296
}
338
297
339
- let v = this . cohort ( ) as Rational ;
298
+ let v = this . cohort ( ) ;
299
+
300
+ if ( v === "0" || v === "-0" ) {
301
+ return this . clone ( ) ;
302
+ }
303
+
340
304
let q = this . quantum ( ) as number ;
341
305
342
306
return new Decimal128 (
@@ -345,10 +309,6 @@ export class Decimal128 {
345
309
}
346
310
347
311
private coefficient ( ) : bigint {
348
- if ( this . isZero ( ) ) {
349
- throw new RangeError ( "Zero does not have a significand" ) ;
350
- }
351
-
352
312
let d = this . d as Decimal ;
353
313
return d . coefficient ( ) ;
354
314
}
@@ -390,11 +350,6 @@ export class Decimal128 {
390
350
}
391
351
392
352
let c = v . scale10 ( 0 - q ) ;
393
-
394
- if ( ! c . isInteger ( ) ) {
395
- throw new TypeError ( "The coefficient is not an integer." ) ;
396
- }
397
-
398
353
let s = c . numerator . toString ( ) ;
399
354
let p = this . _isNegative ? "-" : "" ;
400
355
@@ -446,11 +401,7 @@ export class Decimal128 {
446
401
447
402
if ( ! preserveTrailingZeroes && asDecimalString . match ( / [ . ] / ) ) {
448
403
asDecimalString = asDecimalString . replace ( / 0 + $ / , "" ) ;
449
- if ( asDecimalString === "" ) {
450
- asDecimalString = "0" ;
451
- } else if ( asDecimalString === "-" ) {
452
- asDecimalString = "-0" ;
453
- } else if ( asDecimalString . match ( / [ . ] $ / ) ) {
404
+ if ( asDecimalString . match ( / [ . ] $ / ) ) {
454
405
asDecimalString = asDecimalString . substring (
455
406
0 ,
456
407
asDecimalString . length - 1
@@ -505,22 +456,10 @@ export class Decimal128 {
505
456
506
457
if ( roundedRendered . match ( / [ . ] / ) ) {
507
458
let [ lhs , rhs ] = roundedRendered . split ( / [ . ] / ) ;
508
- if ( rhs . length < n ) {
509
- return lhs + "." + rhs + "0" . repeat ( n - rhs . length ) ;
510
- }
511
-
512
- if ( n === 0 ) {
513
- return lhs ;
514
- }
515
-
516
459
return lhs + "." + rhs . substring ( 0 , n ) ;
517
460
}
518
461
519
- if ( n === 0 ) {
520
- return roundedRendered ;
521
- }
522
-
523
- return roundedRendered + "." + "0" . repeat ( n ) ;
462
+ return roundedRendered ;
524
463
}
525
464
526
465
toPrecision ( opts ?: { digits ?: number } ) : string {
@@ -560,30 +499,16 @@ export class Decimal128 {
560
499
let p = this . isNegative ( ) ? "-" : "" ;
561
500
562
501
if ( n <= lhs . length ) {
563
- if ( lhs . match ( / [ . ] $ / ) ) {
564
- lhs = lhs . substring ( 0 , n ) ;
565
- }
566
-
567
502
if ( lhs . length === n ) {
568
503
return p + lhs ;
569
504
}
570
505
571
- if ( 1 === n ) {
572
- return p + s . substring ( 0 , 1 ) + "e+" + `${ lhs . length - n } ` ;
573
- }
574
-
575
- return (
576
- p +
577
- s . substring ( 0 , 1 ) +
578
- "." +
579
- s . substring ( 1 , n ) +
580
- "e+" +
581
- `${ lhs . length - n + 1 } `
582
- ) ;
506
+ return p + s . substring ( 0 , n ) + "e+" + `${ lhs . length - n + 1 } ` ;
583
507
}
584
508
585
509
if ( n <= lhs . length + rhs . length ) {
586
- return p + s . substring ( 0 , n + 1 ) ; // plus one because of the decimal point
510
+ let rounded = this . round ( n - lhs . length ) ;
511
+ return rounded . emitDecimal ( ) ;
587
512
}
588
513
589
514
return p + lhs + "." + rhs + "0" . repeat ( n - lhs . length - rhs . length ) ;
@@ -860,10 +785,6 @@ export class Decimal128 {
860
785
let preferredQuantum = Math . min ( ourExponent , theirExponent ) ;
861
786
862
787
if ( difference . isZero ( ) ) {
863
- if ( this . _isNegative ) {
864
- difference = "-0" ;
865
- }
866
-
867
788
difference = "0" ;
868
789
}
869
790
@@ -1084,8 +1005,6 @@ export class Decimal128 {
1084
1005
}
1085
1006
1086
1007
let v = this . cohort ( ) as Rational ;
1087
- let q = this . quantum ( ) as number ;
1088
-
1089
1008
let roundedV = v . round ( numDecimalDigits , mode ) ;
1090
1009
1091
1010
if ( roundedV . isZero ( ) ) {
0 commit comments