-
-
Notifications
You must be signed in to change notification settings - Fork 648
/
number.ts
388 lines (296 loc) · 8.13 KB
/
number.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import { itoa32, utoa32, itoa64, utoa64, dtoa } from "./util/number";
import { strtol, strtod } from "./util/string";
// @ts-ignore: decorator
@builtin @inline
export const NaN: f64 = 0 / 0; // context-aware
// @ts-ignore: decorator
@builtin @inline
export const Infinity: f64 = 1 / 0; // context-aware
// @ts-ignore: decorator
@builtin
export declare function isNaN<T extends number>(value: T): bool;
// @ts-ignore: decorator
@builtin
export declare function isFinite<T extends number>(value: T): bool;
@final @unmanaged
export abstract class I8 {
// @ts-ignore: decorator
@lazy
static readonly MIN_VALUE: i8 = i8.MIN_VALUE;
// @ts-ignore: decorator
@lazy
static readonly MAX_VALUE: i8 = i8.MAX_VALUE;
/** @deprecated */
static parseInt(value: string, radix: i32 = 0): i8 {
return <i8>strtol<i32>(value, radix);
}
toString(this: i8, radix: i32 = 10): String {
return itoa32(this, radix);
}
}
@final @unmanaged
export abstract class I16 {
// @ts-ignore: decorator
@lazy
static readonly MIN_VALUE: i16 = i16.MIN_VALUE;
// @ts-ignore: decorator
@lazy
static readonly MAX_VALUE: i16 = i16.MAX_VALUE;
/** @deprecated */
static parseInt(value: string, radix: i32 = 0): i16 {
return <i16>strtol<i32>(value, radix);
}
toString(this: i16, radix: i32 = 10): String {
return itoa32(this, radix);
}
}
@final @unmanaged
export abstract class I32 {
// @ts-ignore: decorator
@lazy
static readonly MIN_VALUE: i32 = i32.MIN_VALUE;
// @ts-ignore: decorator
@lazy
static readonly MAX_VALUE: i32 = i32.MAX_VALUE;
/** @deprecated */
static parseInt(value: string, radix: i32 = 0): i32 {
return <i32>strtol<i32>(value, radix);
}
toString(this: i32, radix: i32 = 10): String {
return itoa32(this, radix);
}
}
@final @unmanaged
export abstract class I64 {
// @ts-ignore: decorator
@lazy
static readonly MIN_VALUE: i64 = i64.MIN_VALUE;
// @ts-ignore: decorator
@lazy
static readonly MAX_VALUE: i64 = i64.MAX_VALUE;
/** @deprecated */
static parseInt(value: string, radix: i32 = 0): i64 {
return strtol<i64>(value, radix);
}
toString(this: i64, radix: i32 = 10): String {
return itoa64(this, radix);
}
}
@final @unmanaged
export abstract class Isize {
// @ts-ignore: decorator
@lazy
static readonly MIN_VALUE: isize = isize.MIN_VALUE;
// @ts-ignore: decorator
@lazy
static readonly MAX_VALUE: isize = isize.MAX_VALUE;
/** @deprecated */
static parseInt(value: string, radix: i32 = 0): isize {
return <isize>strtol<i64>(value, radix);
}
toString(this: isize, radix: i32 = 10): String {
if (sizeof<isize>() == 4) {
return itoa32(<i32>this, radix);
} else {
return itoa64(<i64>this, radix);
}
}
}
@final @unmanaged
export abstract class U8 {
// @ts-ignore: decorator
@lazy
static readonly MIN_VALUE: u8 = u8.MIN_VALUE;
// @ts-ignore: decorator
@lazy
static readonly MAX_VALUE: u8 = u8.MAX_VALUE;
/** @deprecated */
static parseInt(value: string, radix: i32 = 0): u8 {
return <u8>strtol<i32>(value, radix);
}
toString(this: u8, radix: i32 = 10): String {
return utoa32(this, radix);
}
}
@final @unmanaged
export abstract class U16 {
// @ts-ignore: decorator
@lazy
static readonly MIN_VALUE: u16 = u16.MIN_VALUE;
// @ts-ignore: decorator
@lazy
static readonly MAX_VALUE: u16 = u16.MAX_VALUE;
/** @deprecated */
static parseInt(value: string, radix: i32 = 0): u16 {
return <u16>strtol<i32>(value, radix);
}
toString(this: u16, radix: i32 = 10): String {
return utoa32(this, radix);
}
}
@final @unmanaged
export abstract class U32 {
// @ts-ignore: decorator
@lazy
static readonly MIN_VALUE: u32 = u32.MIN_VALUE;
// @ts-ignore: decorator
@lazy
static readonly MAX_VALUE: u32 = u32.MAX_VALUE;
/** @deprecated */
static parseInt(value: string, radix: i32 = 0): u32 {
return <u32>strtol<i32>(value, radix);
}
toString(this: u32, radix: i32 = 10): String {
return utoa32(this, radix);
}
}
@final @unmanaged
export abstract class U64 {
// @ts-ignore: decorator
@lazy
static readonly MIN_VALUE: u64 = u64.MIN_VALUE;
// @ts-ignore: decorator
@lazy
static readonly MAX_VALUE: u64 = u64.MAX_VALUE;
/** @deprecated */
static parseInt(value: string, radix: i32 = 0): u64 {
return <u64>strtol<i64>(value, radix);
}
toString(this: u64, radix: i32 = 10): String {
return utoa64(this, radix);
}
}
@final @unmanaged
export abstract class Usize {
// @ts-ignore: decorator
@lazy
static readonly MIN_VALUE: usize = usize.MIN_VALUE;
// @ts-ignore: decorator
@lazy
static readonly MAX_VALUE: usize = usize.MAX_VALUE;
/** @deprecated */
static parseInt(value: string, radix: i32 = 0): usize {
return <usize>strtol<i64>(value, radix);
}
toString(this: usize, radix: i32 = 10): String {
if (sizeof<usize>() == 4) {
return utoa32(<u32>this, radix);
} else {
return utoa64(<u64>this, radix);
}
}
}
@final @unmanaged
export abstract class Bool {
// @ts-ignore: decorator
@lazy
static readonly MIN_VALUE: bool = bool.MIN_VALUE;
// @ts-ignore: decorator
@lazy
static readonly MAX_VALUE: bool = bool.MAX_VALUE;
toString(this: bool, radix: i32 = 0): String {
return this ? "true" : "false";
}
}
export { Bool as Boolean };
@final @unmanaged
export abstract class F32 {
// @ts-ignore: decorator
@lazy
static readonly EPSILON: f32 = f32.EPSILON;
// @ts-ignore: decorator
@lazy
static readonly MIN_VALUE: f32 = f32.MIN_VALUE;
// @ts-ignore: decorator
@lazy
static readonly MAX_VALUE: f32 = f32.MAX_VALUE;
// @ts-ignore: decorator
@lazy
static readonly MIN_SAFE_INTEGER: f32 = f32.MIN_SAFE_INTEGER;
// @ts-ignore: decorator
@lazy
static readonly MAX_SAFE_INTEGER: f32 = f32.MAX_SAFE_INTEGER;
// @ts-ignore: decorator
@lazy
static readonly POSITIVE_INFINITY: f32 = f32.POSITIVE_INFINITY;
// @ts-ignore: decorator
@lazy
static readonly NEGATIVE_INFINITY: f32 = f32.NEGATIVE_INFINITY;
// @ts-ignore: decorator
@lazy
static readonly NaN: f32 = f32.NaN;
static isNaN(value: f32): bool {
return isNaN<f32>(value);
}
static isFinite(value: f32): bool {
return isFinite<f32>(value);
}
static isSafeInteger(value: f32): bool {
return abs<f32>(value) <= f32.MAX_SAFE_INTEGER && trunc<f32>(value) == value;
}
static isInteger(value: f32): bool {
return isFinite<f32>(value) && trunc<f32>(value) == value;
}
/** @deprecated */
static parseInt(value: string, radix: i32 = 0): f32 {
return <f32>strtol<f64>(value, radix);
}
/** @deprecated */
static parseFloat(value: string): f32 {
return <f32>strtod(value);
}
toString(this: f32, radix: i32 = 0): String {
return dtoa(this);
}
}
@final @unmanaged
export abstract class F64 {
// @ts-ignore: decorator
@lazy
static readonly EPSILON: f64 = f64.EPSILON;
// @ts-ignore: decorator
@lazy
static readonly MIN_VALUE: f64 = f64.MIN_VALUE;
// @ts-ignore: decorator
@lazy
static readonly MAX_VALUE: f64 = f64.MAX_VALUE;
// @ts-ignore: decorator
@lazy
static readonly MIN_SAFE_INTEGER: f64 = f64.MIN_SAFE_INTEGER;
// @ts-ignore: decorator
@lazy
static readonly MAX_SAFE_INTEGER: f64 = f64.MAX_SAFE_INTEGER;
// @ts-ignore: decorator
@lazy
static readonly POSITIVE_INFINITY: f64 = f64.POSITIVE_INFINITY;
// @ts-ignore: decorator
@lazy
static readonly NEGATIVE_INFINITY: f64 = f64.NEGATIVE_INFINITY;
// @ts-ignore: decorator
@lazy
static readonly NaN: f64 = f64.NaN;
static isNaN(value: f64): bool {
return isNaN<f64>(value);
}
static isFinite(value: f64): bool {
return isFinite<f64>(value);
}
static isSafeInteger(value: f64): bool {
return abs<f64>(value) <= f64.MAX_SAFE_INTEGER && trunc<f64>(value) == value;
}
static isInteger(value: f64): bool {
return isFinite<f64>(value) && trunc<f64>(value) == value;
}
/** @deprecated */
static parseInt(value: string, radix: i32 = 0): f64 {
return strtol<f64>(value, radix);
}
/** @deprecated */
static parseFloat(value: string): f64 {
return strtod(value);
}
toString(this: f64, radix: i32 = 0): String {
return dtoa(this);
}
}
export { F64 as Number };