@@ -11,11 +11,7 @@ function _cohort(s: string): "0" | "-0" | Rational {
11
11
return "-0" ;
12
12
}
13
13
14
- if ( c === "-0" ) {
15
- return "0" ;
16
- }
17
-
18
- return c . negate ( ) ;
14
+ return ( c as Rational ) . negate ( ) ;
19
15
}
20
16
21
17
if ( s . match ( / ^ 0 0 + / ) ) {
@@ -26,11 +22,7 @@ function _cohort(s: string): "0" | "-0" | Rational {
26
22
return "0" ;
27
23
}
28
24
29
- if ( s . match ( / ^ - ? 0 [ e E ] [ + - ] ? [ 0 - 9 ] + $ / ) ) {
30
- if ( s . match ( / ^ - / ) ) {
31
- return "-0" ;
32
- }
33
-
25
+ if ( s . match ( / ^ 0 [ e E ] [ + - ] ? [ 0 - 9 ] + $ / ) ) {
34
26
return "0" ;
35
27
}
36
28
@@ -79,7 +71,11 @@ export class Decimal {
79
71
}
80
72
81
73
if ( ! Number . isInteger ( q ) ) {
82
- throw new Error ( "The quantum must be an integer." ) ;
74
+ throw new RangeError ( "The quantum must be an integer." ) ;
75
+ }
76
+
77
+ if ( Object . is ( q , - 0 ) ) {
78
+ throw new RangeError ( "The quantum cannot be negative zero." ) ;
83
79
}
84
80
85
81
if ( v instanceof Rational ) {
@@ -96,144 +92,19 @@ export class Decimal {
96
92
this . quantum = q ;
97
93
}
98
94
99
- public isZero ( ) : boolean {
100
- let v = this . cohort ;
101
- return v === "0" || v === "-0" ;
102
- }
103
-
104
- public isInteger ( ) : boolean {
105
- let v = this . cohort ;
106
-
107
- if ( v === "0" || v === "-0" ) {
108
- return true ;
109
- }
110
-
111
- return v . isInteger ( ) ;
112
- }
113
-
114
- public isNegative ( ) : boolean {
115
- let v = this . cohort ;
116
-
117
- if ( v === "0" ) {
118
- return false ;
119
- }
120
-
121
- if ( v === "-0" ) {
122
- return true ;
123
- }
124
-
125
- return v . isNegative ;
126
- }
127
-
128
- public scale10 ( n : number , adjustQuantum ?: boolean ) : Decimal {
129
- if ( ! Number . isInteger ( n ) ) {
130
- throw new Error ( "The scale factor must be an integer." ) ;
131
- }
132
-
133
- if ( 0 === n ) {
134
- return this ;
135
- }
136
-
137
- let v = this . cohort ;
138
- let newQuantum = this . quantum ;
139
-
140
- if ( typeof adjustQuantum === "boolean" && adjustQuantum ) {
141
- if ( n < 0 ) {
142
- newQuantum -= n ;
143
- } else {
144
- newQuantum += n ;
145
- }
146
- }
147
-
148
- if ( v === "0" || v === "-0" ) {
149
- return new Decimal ( { cohort : v , quantum : newQuantum } ) ;
150
- }
151
-
152
- return new Decimal ( {
153
- cohort : v . scale10 ( n ) ,
154
- quantum : newQuantum ,
155
- } ) ;
156
- }
157
-
158
95
public negate ( ) : Decimal {
159
- let v = this . cohort ;
160
-
161
- if ( v === "0" ) {
162
- return new Decimal ( { cohort : "-0" , quantum : this . quantum } ) ;
163
- }
164
-
165
- if ( v === "-0" ) {
166
- return new Decimal ( { cohort : "0" , quantum : this . quantum } ) ;
167
- }
96
+ let v = this . cohort as Rational ;
168
97
169
98
return new Decimal ( {
170
99
cohort : v . negate ( ) ,
171
100
quantum : this . quantum ,
172
101
} ) ;
173
102
}
174
103
175
- public significand ( ) : Rational {
176
- if ( this . isNegative ( ) ) {
177
- return this . negate ( ) . significand ( ) ;
178
- }
179
-
180
- let v = this . cohort ;
181
-
182
- if ( v === "0" || v === "-0" ) {
183
- throw new RangeError ( "Cannot compute coefficient of zero." ) ;
184
- }
185
-
186
- while ( ratTen . lessThan ( v ) || ratTen . equals ( v ) ) {
187
- v = v . scale10 ( - 1 ) ;
188
- }
189
-
190
- while ( v . lessThan ( ratOne ) ) {
191
- v = v . scale10 ( 1 ) ;
192
- }
193
-
194
- return v ;
195
- }
196
-
197
- public exponent ( ) : number {
198
- if ( this . isNegative ( ) ) {
199
- return this . negate ( ) . exponent ( ) ;
200
- }
201
-
202
- let v = this . cohort ;
203
-
204
- if ( v === "0" || v === "-0" ) {
205
- throw new RangeError ( "Cannot compute coefficient of zero." ) ;
206
- }
207
-
208
- let e = 0 ;
209
-
210
- while ( ratTen . lessThan ( v ) || ratTen . equals ( v ) ) {
211
- v = v . scale10 ( - 1 ) ;
212
- e ++ ;
213
- }
214
-
215
- while ( v . lessThan ( ratOne ) ) {
216
- v = v . scale10 ( 1 ) ;
217
- e -- ;
218
- }
219
-
220
- return e ;
221
- }
222
-
223
104
public coefficient ( ) : bigint {
224
- let v = this . cohort ;
225
-
226
- if ( v === "0" || v === "-0" ) {
227
- throw new RangeError ( "Cannot compute coefficient of zero." ) ;
228
- }
229
-
105
+ let v = this . cohort as Rational ;
230
106
let q = this . quantum ;
231
107
let c = v . scale10 ( 0 - q ) ;
232
-
233
- if ( ! c . isInteger ( ) ) {
234
- throw new TypeError ( "The coefficient is not an integer." ) ;
235
- }
236
-
237
108
return c . numerator ;
238
109
}
239
110
}
0 commit comments