-
Notifications
You must be signed in to change notification settings - Fork 0
/
real.ts
358 lines (310 loc) · 8.34 KB
/
real.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
import {
Dyadic,
dyadicAdd,
dyadicBirthday,
dyadicCompare,
dyadicEq,
dyadicFromBigint,
dyadicFromNumber,
dyadicGe,
dyadicGt,
dyadicLe,
dyadicLongDivision,
dyadicLongDivisionIters,
dyadicLt,
dyadicMult,
dyadicNe,
dyadicNeg,
dyadicSign,
dyadicSub,
} from "./dyadic";
export type Real = number | bigint | Dyadic;
export const realZero: Real = 0n;
export const realOne: Real = 1n;
export const realNegOne: Real = -1n;
export const realIsDyadic = (value: Real): value is Dyadic =>
value instanceof Dyadic;
export const realIsPositive = (value: Real) =>
value instanceof Dyadic ? value.isPositive : value > 0;
export const realIsNegative = (value: Real) =>
value instanceof Dyadic ? value.isNegative : value < 0;
export const realIsOne = (value: Real): boolean =>
value instanceof Dyadic ? value.isOne : value === 1n || value === 1;
export const realIsZero = (value: Real): boolean =>
value instanceof Dyadic ? value.isZero : !value;
export const realToDyadic = (a: Real): Dyadic => {
if (a instanceof Dyadic) {
return a;
}
return typeof a === "bigint" ? dyadicFromBigint(a) : dyadicFromNumber(a);
};
const maybeCoerceDyadic2 = (a: Real, b: Real): [Dyadic, Dyadic] | null => {
if (a instanceof Dyadic || b instanceof Dyadic) {
return [realToDyadic(a), realToDyadic(b)];
}
return null;
};
export const realSign = (value: Real): -1 | 0 | 1 => {
if (value instanceof Dyadic) {
return dyadicSign(value);
}
return realIsZero(value) ? 0 : realIsPositive(value) ? 1 : -1;
};
export const realNeg = <R extends Real = Real>(value: R): R =>
// @ts-ignore will preserve type of input
value instanceof Dyadic ? dyadicNeg(value) : -value;
export const realAdd = (left: Real, right: Real): Real => {
const p = maybeCoerceDyadic2(left, right);
if (p) {
return dyadicAdd(p[0], p[1]);
}
if (typeof left === "bigint" && typeof right === "bigint") {
return left + right;
}
return Number(left) + Number(right);
};
export const realSub = (left: Real, right: Real): Real => {
const p = maybeCoerceDyadic2(left, right);
if (p) {
return dyadicSub(p[0], p[1]);
}
if (typeof left === "bigint" && typeof right === "bigint") {
return left - right;
}
return Number(left) - Number(right);
};
export const realMult = (left: Real, right: Real): Real => {
const p = maybeCoerceDyadic2(left, right);
if (p) {
return dyadicMult(p[0], p[1]);
}
if (typeof left === "bigint" && typeof right === "bigint") {
return left * right;
}
return Number(left) * Number(right);
};
export const realIntegerDiv = (left: Real, right: Real): Real => {
const p = maybeCoerceDyadic2(left, right);
if (p) {
return Math.floor(Number(p[0]) / Number(p[1]));
}
if (typeof left === "bigint" && typeof right === "bigint") {
return left / right;
}
return Math.floor(Number(left) / Number(right));
};
export const realIntegerPow = (left: Real, right: Real): Real => {
// TODO handle dyadics
if (typeof left === "bigint" && typeof right === "bigint") {
return left ** right;
}
return Number(left) ** Number(right);
};
export const realLongDivision = (left: Real, right: Real): [Real, Real] =>
dyadicLongDivision(realToDyadic(left), realToDyadic(right));
export const realLongDivisionLossy = (
left: Real,
right: Real,
): [Real, Real] => {
if (typeof left === "number" || typeof right === "number") {
return [Number(left) / Number(right), 0];
}
return dyadicLongDivision(realToDyadic(left), realToDyadic(right));
};
export const realLongDivisionIters = (
left: Real,
right: Real,
iters = 1n as number | bigint,
): [Real, Real] =>
dyadicLongDivisionIters(realToDyadic(left), realToDyadic(right), iters);
export const realCompare = (left: Real, right: Real): -1 | 0 | 1 => {
if (left === right) {
return 0;
}
const p = maybeCoerceDyadic2(left, right);
if (p) {
return dyadicCompare(p[0], p[1]);
}
if (typeof left === "bigint" && typeof right === "bigint") {
return right > left ? 1 : -1;
}
const nr = Number(right);
const nl = Number(left);
return nl === nr ? 0 : nr > nl ? 1 : -1;
};
export const realEq = (left: Real, right: Real): boolean => {
if (left === right) {
return true;
}
const p = maybeCoerceDyadic2(left, right);
if (p) {
return dyadicEq(p[0], p[1]);
}
return typeof left === "bigint" && typeof right === "bigint"
? left === right
: Number(left) === Number(right);
};
export const realNe = (left: Real, right: Real): boolean => {
if (left === right) {
return false;
}
const p = maybeCoerceDyadic2(left, right);
if (p) {
return dyadicNe(p[0], p[1]);
}
return typeof left === "bigint" && typeof right === "bigint"
? left !== right
: Number(left) !== Number(right);
};
export const realGt = (left: Real, right: Real): boolean => {
if (left === right) {
return false;
}
const p = maybeCoerceDyadic2(left, right);
if (p) {
return dyadicGt(p[0], p[1]);
}
return left > right;
};
export const realGe = (left: Real, right: Real): boolean => {
if (left === right) {
return true;
}
const p = maybeCoerceDyadic2(left, right);
if (p) {
return dyadicGe(p[0], p[1]);
}
return left >= right;
};
export const realLt = (left: Real, right: Real): boolean => {
if (left === right) {
return false;
}
const p = maybeCoerceDyadic2(left, right);
if (p) {
return dyadicLt(p[0], p[1]);
}
return left < right;
};
export const realLe = (left: Real, right: Real): boolean => {
if (left === right) {
return true;
}
const p = maybeCoerceDyadic2(left, right);
if (p) {
return dyadicLe(p[0], p[1]);
}
return left <= right;
};
export const realAbs = <R extends Real = Real>(value: R): R =>
realIsNegative(value) ? realNeg(value) : value;
export const realFloorToBigint = (value: Real): bigint =>
value instanceof Dyadic
? value.isNegative
? value.bigintQuotient + 1n
: value.bigintQuotient
: typeof value === "bigint"
? value
: BigInt(Math.floor(value));
export const realCeilingToBigint = (value: Real): bigint =>
realIsInteger(value)
? realFloorToBigint(value)
: realFloorToBigint(value) + 1n;
export const realIsInteger = (value: Real): boolean =>
value instanceof Dyadic
? value.isInteger
: typeof value === "bigint"
? true
: Number.isInteger(value);
export const realToBigint = (value: Real): bigint => {
if (value instanceof Dyadic) {
if (!value.isInteger) {
throw new RangeError("Dyadic is not an integer");
}
return value.bigintQuotient;
}
return BigInt(value);
};
export const realToNumber = (value: Real): number => Number(value);
/**
* Determines the birthday of a bigint or number.
* Floating point numbers are treated as exact values and since they
* are in the form of (integer * 2^exponent), floating point numbers
* have a finite birthday.
*/
export const realBirthday = (value: Real): bigint => {
if (value instanceof Dyadic) {
return dyadicBirthday(value);
}
if (realIsZero(value)) {
return 0n;
}
if (typeof value === "bigint") {
return value < 0n ? -value : value;
}
if (typeof value === "number" && Number.isInteger(value)) {
return BigInt(value < 0 ? -value : value);
}
return dyadicBirthday(dyadicFromNumber(value));
};
export const realToJson = (
value: Real,
preserveBigint = false,
): string | bigint | unknown => {
if (value instanceof Dyadic) {
return preserveBigint
? { dn: value.numerator, dp: value.power }
: {
dn: `${value.numerator}n`,
dp: `${value.power}`,
};
}
if (typeof value === "bigint") {
return preserveBigint ? value : `${value}n`;
}
return `${value}`;
};
export const realToString = (value: Real) => {
if (value instanceof Dyadic) {
return value.toString();
}
return `${value}`;
};
const EQ_HASH_CACHE = new Map<Real, number>([]);
export const realEqHash = (value: Real): number => {
if (realIsZero(value)) {
return 0;
}
const found = EQ_HASH_CACHE.get(value);
if (typeof found === "number") {
return found;
}
const s = realToString(value) as string;
const MASK = 0xffff_ffff;
const MULT = 31;
let h = 0;
for (let i = 0; i < s.length; i++) {
h = MULT * h + s.charCodeAt(i);
h = h & MASK;
}
if (typeof value === "bigint" && value > -256n && value < 256n) {
EQ_HASH_CACHE.set(value, h);
} else if (
typeof value === "number" &&
Number.isInteger(value) &&
value > -256 &&
value < 256
) {
EQ_HASH_CACHE.set(value, h);
}
return h;
};
export const hasRealType = (x: unknown): x is Real => {
if (typeof x === "number" || typeof x === "bigint") {
return true;
}
if (x instanceof Dyadic) {
return true;
}
return false;
};