@@ -54,12 +54,7 @@ function adjustDecimal128(v: Rational, q: number): Decimal {
54
54
55
55
let x = new Decimal ( { cohort : v , quantum : q } ) ;
56
56
57
- if (
58
- v
59
- . abs ( )
60
- . scale10 ( 0 - q )
61
- . cmp ( TEN_MAX_EXPONENT ) <= 0
62
- ) {
57
+ if ( v . scale10 ( 0 - q ) . cmp ( TEN_MAX_EXPONENT ) <= 0 ) {
63
58
return x ;
64
59
}
65
60
@@ -76,6 +71,10 @@ function adjustDecimal128(v: Rational, q: number): Decimal {
76
71
let cAsString = c . toFixed ( Infinity ) ;
77
72
let [ integerPart , fractionalPart ] = cAsString . split ( / [ . ] / ) ;
78
73
74
+ if ( integerPart === "0" ) {
75
+ integerPart = "" ;
76
+ }
77
+
79
78
if ( integerPart . length > MAX_SIGNIFICANT_DIGITS ) {
80
79
throw new RangeError ( "Integer part too large" ) ;
81
80
}
@@ -267,7 +266,7 @@ export class Decimal128 {
267
266
}
268
267
269
268
if ( this . isNegative ( ) ) {
270
- let [ s , e ] = this . neg ( ) . significandAndExponent ( ) ;
269
+ let [ s , e ] = this . negate ( ) . significandAndExponent ( ) ;
271
270
return [ s . negate ( ) , e ] ;
272
271
}
273
272
@@ -302,7 +301,7 @@ export class Decimal128 {
302
301
}
303
302
304
303
if ( this . isNegative ( ) ) {
305
- return this . neg ( ) . mantissa ( ) . neg ( ) ;
304
+ return this . negate ( ) . mantissa ( ) . negate ( ) ;
306
305
}
307
306
308
307
let x : Decimal128 = this ;
@@ -345,7 +344,7 @@ export class Decimal128 {
345
344
) ;
346
345
}
347
346
348
- private significand ( ) : bigint {
347
+ private coefficient ( ) : bigint {
349
348
if ( this . isZero ( ) ) {
350
349
throw new RangeError ( "Zero does not have a significand" ) ;
351
350
}
@@ -722,14 +721,14 @@ export class Decimal128 {
722
721
723
722
if ( ! this . isFinite ( ) ) {
724
723
if ( this . isNegative ( ) ) {
725
- return this . neg ( ) ;
724
+ return this . negate ( ) ;
726
725
}
727
726
728
727
return this . clone ( ) ;
729
728
}
730
729
731
730
if ( this . isNegative ( ) ) {
732
- return this . neg ( ) ;
731
+ return this . negate ( ) ;
733
732
}
734
733
735
734
return this . clone ( ) ;
@@ -762,7 +761,7 @@ export class Decimal128 {
762
761
}
763
762
764
763
if ( this . isNegative ( ) && x . isNegative ( ) ) {
765
- return this . neg ( ) . add ( x . neg ( ) ) . neg ( ) ;
764
+ return this . negate ( ) . add ( x . negate ( ) ) . negate ( ) ;
766
765
}
767
766
768
767
if ( this . isZero ( ) ) {
@@ -819,15 +818,15 @@ export class Decimal128 {
819
818
}
820
819
821
820
if ( ! x . isFinite ( ) ) {
822
- return x . neg ( ) ;
821
+ return x . negate ( ) ;
823
822
}
824
823
825
824
if ( x . isNegative ( ) ) {
826
- return this . add ( x . neg ( ) ) ;
825
+ return this . add ( x . negate ( ) ) ;
827
826
}
828
827
829
828
if ( this . isZero ( ) ) {
830
- return x . neg ( ) ;
829
+ return x . negate ( ) ;
831
830
}
832
831
833
832
if ( x . isZero ( ) ) {
@@ -897,11 +896,11 @@ export class Decimal128 {
897
896
}
898
897
899
898
if ( this . isNegative ( ) ) {
900
- return this . neg ( ) . multiply ( x ) . neg ( ) ;
899
+ return this . negate ( ) . multiply ( x ) . negate ( ) ;
901
900
}
902
901
903
902
if ( x . isNegative ( ) ) {
904
- return this . multiply ( x . neg ( ) ) . neg ( ) ;
903
+ return this . multiply ( x . negate ( ) ) . negate ( ) ;
905
904
}
906
905
907
906
let ourCohort = this . cohort ( ) as Rational ;
@@ -940,7 +939,19 @@ export class Decimal128 {
940
939
}
941
940
942
941
private clone ( ) : Decimal128 {
943
- return new Decimal128 ( this . toString ( ) ) ;
942
+ if ( this . isNaN ( ) ) {
943
+ return new Decimal128 ( NAN ) ;
944
+ }
945
+
946
+ if ( ! this . isFinite ( ) ) {
947
+ return new Decimal128 (
948
+ this . isNegative ( ) ? NEGATIVE_INFINITY : POSITIVE_INFINITY
949
+ ) ;
950
+ }
951
+
952
+ return new Decimal128 (
953
+ new Decimal ( { cohort : this . cohort ( ) , quantum : this . quantum ( ) } )
954
+ ) ;
944
955
}
945
956
946
957
/**
@@ -953,14 +964,14 @@ export class Decimal128 {
953
964
return new Decimal128 ( NAN ) ;
954
965
}
955
966
956
- if ( this . isZero ( ) ) {
957
- return this . clone ( ) ;
958
- }
959
-
960
967
if ( x . isZero ( ) ) {
961
968
return new Decimal128 ( NAN ) ;
962
969
}
963
970
971
+ if ( this . isZero ( ) ) {
972
+ return this . clone ( ) ;
973
+ }
974
+
964
975
if ( ! this . isFinite ( ) ) {
965
976
if ( ! x . isFinite ( ) ) {
966
977
return new Decimal128 ( NAN ) ;
@@ -986,16 +997,16 @@ export class Decimal128 {
986
997
}
987
998
988
999
if ( this . isNegative ( ) ) {
989
- return this . neg ( ) . divide ( x ) . neg ( ) ;
1000
+ return this . negate ( ) . divide ( x ) . negate ( ) ;
990
1001
}
991
1002
992
1003
if ( x . isNegative ( ) ) {
993
- return this . divide ( x . neg ( ) ) . neg ( ) ;
1004
+ return this . divide ( x . negate ( ) ) . negate ( ) ;
994
1005
}
995
1006
996
1007
let adjust = 0 ;
997
- let dividendCoefficient = this . significand ( ) ;
998
- let divisorCoefficient = x . significand ( ) ;
1008
+ let dividendCoefficient = this . coefficient ( ) ;
1009
+ let divisorCoefficient = x . coefficient ( ) ;
999
1010
1000
1011
if ( dividendCoefficient !== 0n ) {
1001
1012
while ( dividendCoefficient < divisorCoefficient ) {
@@ -1029,7 +1040,9 @@ export class Decimal128 {
1029
1040
}
1030
1041
}
1031
1042
1032
- let resultExponent = this . exponent ( ) - ( x . exponent ( ) + adjust ) ;
1043
+ let ourExponent = this . quantum ( ) ;
1044
+ let theirExponent = x . quantum ( ) ;
1045
+ let resultExponent = ourExponent - ( theirExponent + adjust ) ;
1033
1046
return new Decimal128 ( `${ resultCoefficient } E${ resultExponent } ` ) ;
1034
1047
}
1035
1048
@@ -1061,7 +1074,7 @@ export class Decimal128 {
1061
1074
return new Decimal128 ( new Decimal ( { cohort : roundedV , quantum : q } ) ) ;
1062
1075
}
1063
1076
1064
- neg ( ) : Decimal128 {
1077
+ negate ( ) : Decimal128 {
1065
1078
if ( this . isNaN ( ) ) {
1066
1079
return this . clone ( ) ;
1067
1080
}
@@ -1106,11 +1119,11 @@ export class Decimal128 {
1106
1119
}
1107
1120
1108
1121
if ( this . isNegative ( ) ) {
1109
- return this . neg ( ) . remainder ( d ) . neg ( ) ;
1122
+ return this . negate ( ) . remainder ( d ) . negate ( ) ;
1110
1123
}
1111
1124
1112
1125
if ( d . isNegative ( ) ) {
1113
- return this . remainder ( d . neg ( ) ) ;
1126
+ return this . remainder ( d . negate ( ) ) ;
1114
1127
}
1115
1128
1116
1129
if ( ! this . isFinite ( ) ) {
0 commit comments