not-so-float 0.0.1 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +16 -18
- package/README.md +223 -0
- package/dist/index.cjs +776 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +121 -2
- package/dist/index.d.ts +121 -2
- package/dist/index.js +696 -4
- package/dist/index.js.map +1 -1
- package/package.json +12 -5
package/dist/index.js
CHANGED
|
@@ -1,8 +1,700 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// src/nextafter.ts
|
|
2
|
+
var buffer = new ArrayBuffer(8);
|
|
3
|
+
var floatView = new Float64Array(buffer);
|
|
4
|
+
var intView = new BigUint64Array(buffer);
|
|
5
|
+
function next(x) {
|
|
6
|
+
if (x === Infinity) return x;
|
|
7
|
+
if (x === 0) return Number.MIN_VALUE;
|
|
8
|
+
floatView[0] = x;
|
|
9
|
+
let bits = intView[0];
|
|
10
|
+
if (x > 0) {
|
|
11
|
+
bits += 1n;
|
|
12
|
+
} else {
|
|
13
|
+
bits -= 1n;
|
|
14
|
+
}
|
|
15
|
+
intView[0] = bits;
|
|
16
|
+
return floatView[0];
|
|
4
17
|
}
|
|
18
|
+
function prev(x) {
|
|
19
|
+
if (x === -Infinity) return x;
|
|
20
|
+
if (x === 0) return -Number.MIN_VALUE;
|
|
21
|
+
floatView[0] = x;
|
|
22
|
+
let bits = intView[0];
|
|
23
|
+
if (x < 0) {
|
|
24
|
+
bits += 1n;
|
|
25
|
+
} else {
|
|
26
|
+
bits -= 1n;
|
|
27
|
+
}
|
|
28
|
+
intView[0] = bits;
|
|
29
|
+
return floatView[0];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// src/interval.ts
|
|
33
|
+
var defaultNumbers = (x) => x.toString();
|
|
34
|
+
var Interval = class {
|
|
35
|
+
constructor(lo, hi) {
|
|
36
|
+
this.lo = lo;
|
|
37
|
+
this.hi = hi;
|
|
38
|
+
}
|
|
39
|
+
isFull() {
|
|
40
|
+
return this.lo === -Infinity && this.hi === Infinity;
|
|
41
|
+
}
|
|
42
|
+
isFinite() {
|
|
43
|
+
return isFinite(this.lo) && isFinite(this.hi);
|
|
44
|
+
}
|
|
45
|
+
isDegenerate() {
|
|
46
|
+
return this.lo === this.hi;
|
|
47
|
+
}
|
|
48
|
+
contains(value) {
|
|
49
|
+
return this.lo <= value && value <= this.hi;
|
|
50
|
+
}
|
|
51
|
+
superset(other) {
|
|
52
|
+
return this.contains(other.lo) && this.contains(other.hi);
|
|
53
|
+
}
|
|
54
|
+
subset(other) {
|
|
55
|
+
return other.superset(this);
|
|
56
|
+
}
|
|
57
|
+
toString(numbers = defaultNumbers) {
|
|
58
|
+
return `[${numbers(this.lo)}, ${numbers(this.hi)}]`;
|
|
59
|
+
}
|
|
60
|
+
print() {
|
|
61
|
+
console.log(this.toString());
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
function typeCheckIntervalBound(v) {
|
|
65
|
+
if (typeof v !== "number") {
|
|
66
|
+
throw TypeError(`Interval bounds must be a number, got ${typeof v}`);
|
|
67
|
+
}
|
|
68
|
+
if (isNaN(v)) {
|
|
69
|
+
throw TypeError(`Interval bounds must not be NaN`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function interval(a, b) {
|
|
73
|
+
if (typeof b === "undefined") {
|
|
74
|
+
b = a;
|
|
75
|
+
}
|
|
76
|
+
typeCheckIntervalBound(a);
|
|
77
|
+
typeCheckIntervalBound(b);
|
|
78
|
+
if (a > b) {
|
|
79
|
+
throw TypeError("Interval bound must be such that lo <= hi");
|
|
80
|
+
}
|
|
81
|
+
if (a === Infinity) {
|
|
82
|
+
throw TypeError("Interval lower bound cannot be Infinity");
|
|
83
|
+
}
|
|
84
|
+
if (b === -Infinity) {
|
|
85
|
+
throw TypeError("Interval lower bound cannot be -Infinity");
|
|
86
|
+
}
|
|
87
|
+
return new Interval(a, b);
|
|
88
|
+
}
|
|
89
|
+
var inter = interval;
|
|
90
|
+
var FULL = new Interval(-Infinity, Infinity);
|
|
91
|
+
function typeCheckIsInterval(x) {
|
|
92
|
+
if (!(x instanceof Interval)) {
|
|
93
|
+
throw TypeError(`Argument must be an interval (got type ${typeof x})`);
|
|
94
|
+
}
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// src/union.ts
|
|
99
|
+
var Union = class {
|
|
100
|
+
constructor(intervals) {
|
|
101
|
+
this.intervals = intervals;
|
|
102
|
+
}
|
|
103
|
+
isEmpty() {
|
|
104
|
+
return this.intervals.length === 0;
|
|
105
|
+
}
|
|
106
|
+
lower() {
|
|
107
|
+
if (this.isEmpty()) return Infinity;
|
|
108
|
+
return this.intervals[0].lo;
|
|
109
|
+
}
|
|
110
|
+
upper() {
|
|
111
|
+
if (this.isEmpty()) return -Infinity;
|
|
112
|
+
return this.intervals[this.intervals.length - 1].hi;
|
|
113
|
+
}
|
|
114
|
+
contains(value) {
|
|
115
|
+
return this.intervals.some((i) => i.contains(value));
|
|
116
|
+
}
|
|
117
|
+
forEach(callback) {
|
|
118
|
+
this.intervals.forEach(callback);
|
|
119
|
+
}
|
|
120
|
+
toString(numbers = defaultNumbers) {
|
|
121
|
+
if (this.intervals.length === 0) return "{\u2205}";
|
|
122
|
+
return this.intervals.map((i) => i.toString(numbers)).join(" U ");
|
|
123
|
+
}
|
|
124
|
+
print() {
|
|
125
|
+
console.log(this.toString());
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
function union(intervals) {
|
|
129
|
+
if (intervals.length <= 1) {
|
|
130
|
+
return new Union(intervals);
|
|
131
|
+
}
|
|
132
|
+
intervals.sort((x, y) => x.lo - y.lo);
|
|
133
|
+
const merged = [intervals[0]];
|
|
134
|
+
for (let i = 1; i < intervals.length; i++) {
|
|
135
|
+
const last = merged[merged.length - 1];
|
|
136
|
+
const curr = intervals[i];
|
|
137
|
+
if (curr.lo <= last.hi) {
|
|
138
|
+
merged[merged.length - 1] = new Interval(last.lo, Math.max(last.hi, curr.hi));
|
|
139
|
+
} else {
|
|
140
|
+
merged.push(curr);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return new Union(merged);
|
|
144
|
+
}
|
|
145
|
+
function typeCheckIsUnion(U) {
|
|
146
|
+
if (!(U instanceof Union)) {
|
|
147
|
+
throw TypeError(`Argument must be a Union (got type ${typeof U})`);
|
|
148
|
+
}
|
|
149
|
+
return true;
|
|
150
|
+
}
|
|
151
|
+
function toUnion(x) {
|
|
152
|
+
if (x instanceof Interval) return new Union([x]);
|
|
153
|
+
return x;
|
|
154
|
+
}
|
|
155
|
+
function makeUnaryOpUnion(op) {
|
|
156
|
+
return (U) => {
|
|
157
|
+
typeCheckIsUnion(U);
|
|
158
|
+
const results = [];
|
|
159
|
+
U.forEach((X) => {
|
|
160
|
+
results.push(...op(X).intervals);
|
|
161
|
+
});
|
|
162
|
+
return union(results);
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
function makeBinaryOpUnion(op) {
|
|
166
|
+
return (U, V) => {
|
|
167
|
+
typeCheckIsUnion(U);
|
|
168
|
+
typeCheckIsUnion(V);
|
|
169
|
+
const results = [];
|
|
170
|
+
U.forEach((X, i) => {
|
|
171
|
+
V.forEach((Y) => {
|
|
172
|
+
results.push(...op(X, Y).intervals);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
return union(results);
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function isIntervalOrUnion(x) {
|
|
179
|
+
return x instanceof Interval || x instanceof Union;
|
|
180
|
+
}
|
|
181
|
+
function typeCheckIsIntervalOrUnion(x) {
|
|
182
|
+
if (!isIntervalOrUnion(x)) {
|
|
183
|
+
throw TypeError("Argument must be of type Interval or Union");
|
|
184
|
+
}
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
function makeUnaryOpEither(op) {
|
|
188
|
+
return (A) => {
|
|
189
|
+
typeCheckIsIntervalOrUnion(A);
|
|
190
|
+
return op(toUnion(A));
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
function makeBinaryOpEither(op) {
|
|
194
|
+
return (A, B) => {
|
|
195
|
+
typeCheckIsIntervalOrUnion(A);
|
|
196
|
+
typeCheckIsIntervalOrUnion(B);
|
|
197
|
+
return op(toUnion(A), toUnion(B));
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
function bounded(x) {
|
|
201
|
+
return new Union([new Interval(prev(x), next(x))]);
|
|
202
|
+
}
|
|
203
|
+
var EMPTY = new Union([]);
|
|
204
|
+
|
|
205
|
+
// src/compare.ts
|
|
206
|
+
function ioverlap(a, b) {
|
|
207
|
+
return a.lo <= b.lo && b.lo <= a.hi || b.lo <= a.lo && a.lo <= b.hi;
|
|
208
|
+
}
|
|
209
|
+
function idisjoint(a, b) {
|
|
210
|
+
return a.hi < b.lo || a.lo > b.hi;
|
|
211
|
+
}
|
|
212
|
+
function iuoverlap(X, U) {
|
|
213
|
+
return U.intervals.some((Y) => ioverlap(X, Y));
|
|
214
|
+
}
|
|
215
|
+
function iudisjoint(X, U) {
|
|
216
|
+
return U.intervals.every((Y) => idisjoint(X, Y));
|
|
217
|
+
}
|
|
218
|
+
function overlap(A, B) {
|
|
219
|
+
typeCheckIsIntervalOrUnion(A);
|
|
220
|
+
typeCheckIsIntervalOrUnion(B);
|
|
221
|
+
const Bunion = toUnion(B);
|
|
222
|
+
return toUnion(A).intervals.some((X) => iuoverlap(X, Bunion));
|
|
223
|
+
}
|
|
224
|
+
function disjoint(A, B) {
|
|
225
|
+
typeCheckIsIntervalOrUnion(A);
|
|
226
|
+
typeCheckIsIntervalOrUnion(B);
|
|
227
|
+
const Bunion = toUnion(B);
|
|
228
|
+
return toUnion(A).intervals.every((X) => iudisjoint(X, Bunion));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// src/arithmetic.ts
|
|
232
|
+
function leftAdd(a, b) {
|
|
233
|
+
if (a === 0) {
|
|
234
|
+
return b;
|
|
235
|
+
} else if (b === 0) {
|
|
236
|
+
return a;
|
|
237
|
+
}
|
|
238
|
+
return prev(a + b);
|
|
239
|
+
}
|
|
240
|
+
function rightAdd(a, b) {
|
|
241
|
+
if (a === 0) {
|
|
242
|
+
return b;
|
|
243
|
+
} else if (b === 0) {
|
|
244
|
+
return a;
|
|
245
|
+
}
|
|
246
|
+
return next(a + b);
|
|
247
|
+
}
|
|
248
|
+
function iadd(x, y) {
|
|
249
|
+
typeCheckIsInterval(x);
|
|
250
|
+
typeCheckIsInterval(y);
|
|
251
|
+
return new Union([new Interval(leftAdd(x.lo, y.lo), rightAdd(x.hi, y.hi))]);
|
|
252
|
+
}
|
|
253
|
+
function ineg(x) {
|
|
254
|
+
typeCheckIsInterval(x);
|
|
255
|
+
return new Interval(-x.hi, -x.lo);
|
|
256
|
+
}
|
|
257
|
+
function isub(x, y) {
|
|
258
|
+
return iadd(x, ineg(y));
|
|
259
|
+
}
|
|
260
|
+
function leftMul(a, b) {
|
|
261
|
+
if (a === 0 || b === 0) {
|
|
262
|
+
return 0;
|
|
263
|
+
}
|
|
264
|
+
return prev(a * b);
|
|
265
|
+
}
|
|
266
|
+
function rightMul(a, b) {
|
|
267
|
+
if (a === 0 || b === 0) {
|
|
268
|
+
return 0;
|
|
269
|
+
}
|
|
270
|
+
return next(a * b);
|
|
271
|
+
}
|
|
272
|
+
function imul(x, y) {
|
|
273
|
+
typeCheckIsInterval(x);
|
|
274
|
+
typeCheckIsInterval(y);
|
|
275
|
+
const [a, b] = [x.lo, x.hi];
|
|
276
|
+
const [c, d] = [y.lo, y.hi];
|
|
277
|
+
if (b < 0) {
|
|
278
|
+
if (d < 0) {
|
|
279
|
+
return new Union([new Interval(leftMul(b, d), rightMul(a, c))]);
|
|
280
|
+
} else if (c > 0) {
|
|
281
|
+
return new Union([new Interval(leftMul(a, d), rightMul(b, c))]);
|
|
282
|
+
} else {
|
|
283
|
+
return new Union([new Interval(leftMul(a, d), rightMul(a, c))]);
|
|
284
|
+
}
|
|
285
|
+
} else if (a > 0) {
|
|
286
|
+
if (d < 0) {
|
|
287
|
+
return new Union([new Interval(leftMul(b, c), rightMul(a, d))]);
|
|
288
|
+
} else if (c > 0) {
|
|
289
|
+
return new Union([new Interval(leftMul(a, c), rightMul(b, d))]);
|
|
290
|
+
} else {
|
|
291
|
+
return new Union([new Interval(leftMul(b, c), rightMul(b, d))]);
|
|
292
|
+
}
|
|
293
|
+
} else {
|
|
294
|
+
if (d < 0) {
|
|
295
|
+
return new Union([new Interval(leftMul(b, c), rightMul(a, c))]);
|
|
296
|
+
} else if (c > 0) {
|
|
297
|
+
return new Union([new Interval(leftMul(a, d), rightMul(b, d))]);
|
|
298
|
+
} else {
|
|
299
|
+
return new Union([
|
|
300
|
+
new Interval(
|
|
301
|
+
Math.min(leftMul(a, d), leftMul(b, c)),
|
|
302
|
+
Math.max(rightMul(a, c), rightMul(b, d))
|
|
303
|
+
)
|
|
304
|
+
]);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function leftDiv(a, b) {
|
|
309
|
+
if (b === Infinity) {
|
|
310
|
+
return Math.sign(a) * 0;
|
|
311
|
+
}
|
|
312
|
+
if (b === -Infinity) {
|
|
313
|
+
return Math.sign(-a) * 0;
|
|
314
|
+
}
|
|
315
|
+
return prev(a / b);
|
|
316
|
+
}
|
|
317
|
+
function rightDiv(a, b) {
|
|
318
|
+
if (b === Infinity) {
|
|
319
|
+
return Math.sign(a) * 0;
|
|
320
|
+
}
|
|
321
|
+
if (b === -Infinity) {
|
|
322
|
+
return Math.sign(-a) * 0;
|
|
323
|
+
}
|
|
324
|
+
return next(a / b);
|
|
325
|
+
}
|
|
326
|
+
function intervalType(x) {
|
|
327
|
+
if (x.hi < 0) return 4 /* N1 */;
|
|
328
|
+
if (x.lo > 0) return 0 /* P1 */;
|
|
329
|
+
if (x.lo < 0 && x.hi === 0) return 3 /* N0 */;
|
|
330
|
+
if (x.lo === 0 && x.hi > 0) return 1 /* P0 */;
|
|
331
|
+
if (x.lo === 0 && x.hi === 0) return 5 /* Z */;
|
|
332
|
+
else return 2 /* M */;
|
|
333
|
+
}
|
|
334
|
+
function idiv(x, y) {
|
|
335
|
+
typeCheckIsInterval(x);
|
|
336
|
+
typeCheckIsInterval(y);
|
|
337
|
+
const xType = intervalType(x);
|
|
338
|
+
const yType = intervalType(y);
|
|
339
|
+
if (yType === 5 /* Z */) {
|
|
340
|
+
return EMPTY;
|
|
341
|
+
}
|
|
342
|
+
if (xType === 5 /* Z */) {
|
|
343
|
+
return union([inter(0, 0)]);
|
|
344
|
+
}
|
|
345
|
+
const [a, b] = [x.lo, x.hi];
|
|
346
|
+
const [c, d] = [y.lo, y.hi];
|
|
347
|
+
if (yType === 0 /* P1 */) {
|
|
348
|
+
if (xType === 0 /* P1 */) {
|
|
349
|
+
return union([new Interval(leftDiv(a, d), rightDiv(b, c))]);
|
|
350
|
+
} else if (xType === 1 /* P0 */) {
|
|
351
|
+
return union([new Interval(0, rightDiv(b, c))]);
|
|
352
|
+
} else if (xType === 2 /* M */) {
|
|
353
|
+
return union([new Interval(leftDiv(a, c), rightDiv(b, c))]);
|
|
354
|
+
} else if (xType === 3 /* N0 */) {
|
|
355
|
+
return union([new Interval(leftDiv(a, c), -0)]);
|
|
356
|
+
} else {
|
|
357
|
+
return union([new Interval(leftDiv(a, c), rightDiv(b, d))]);
|
|
358
|
+
}
|
|
359
|
+
} else if (yType === 1 /* P0 */) {
|
|
360
|
+
if (xType === 0 /* P1 */) {
|
|
361
|
+
return union([new Interval(leftDiv(a, d), Infinity)]);
|
|
362
|
+
} else if (xType === 1 /* P0 */) {
|
|
363
|
+
return union([new Interval(0, Infinity)]);
|
|
364
|
+
} else if (xType === 2 /* M */) {
|
|
365
|
+
return union([FULL]);
|
|
366
|
+
} else if (xType === 3 /* N0 */) {
|
|
367
|
+
return union([new Interval(-Infinity, -0)]);
|
|
368
|
+
} else {
|
|
369
|
+
return union([new Interval(-Infinity, rightDiv(b, d))]);
|
|
370
|
+
}
|
|
371
|
+
} else if (yType === 2 /* M */) {
|
|
372
|
+
if (xType === 0 /* P1 */) {
|
|
373
|
+
return union([
|
|
374
|
+
new Interval(-Infinity, rightDiv(a, c)),
|
|
375
|
+
new Interval(leftDiv(a, d), Infinity)
|
|
376
|
+
]);
|
|
377
|
+
} else if (xType == 4 /* N1 */) {
|
|
378
|
+
return union([
|
|
379
|
+
new Interval(-Infinity, rightDiv(b, d)),
|
|
380
|
+
new Interval(leftDiv(b, c), Infinity)
|
|
381
|
+
]);
|
|
382
|
+
} else {
|
|
383
|
+
return union([FULL]);
|
|
384
|
+
}
|
|
385
|
+
} else if (yType === 3 /* N0 */) {
|
|
386
|
+
if (xType === 0 /* P1 */) {
|
|
387
|
+
return union([new Interval(-Infinity, rightDiv(a, c))]);
|
|
388
|
+
} else if (xType === 1 /* P0 */) {
|
|
389
|
+
return union([new Interval(-Infinity, -0)]);
|
|
390
|
+
} else if (xType === 2 /* M */) {
|
|
391
|
+
return union([FULL]);
|
|
392
|
+
} else if (xType === 3 /* N0 */) {
|
|
393
|
+
return union([new Interval(0, Infinity)]);
|
|
394
|
+
} else {
|
|
395
|
+
return union([new Interval(leftDiv(b, c), Infinity)]);
|
|
396
|
+
}
|
|
397
|
+
} else {
|
|
398
|
+
if (xType === 0 /* P1 */) {
|
|
399
|
+
return union([new Interval(leftDiv(b, d), rightDiv(a, c))]);
|
|
400
|
+
} else if (xType === 1 /* P0 */) {
|
|
401
|
+
return union([new Interval(leftDiv(b, d), -0)]);
|
|
402
|
+
} else if (xType === 2 /* M */) {
|
|
403
|
+
return union([new Interval(leftDiv(b, d), rightDiv(a, d))]);
|
|
404
|
+
} else if (xType === 3 /* N0 */) {
|
|
405
|
+
return union([new Interval(0, rightDiv(a, d))]);
|
|
406
|
+
} else {
|
|
407
|
+
return union([new Interval(leftDiv(b, c), rightDiv(a, d))]);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
var uadd = makeBinaryOpUnion(iadd);
|
|
412
|
+
var usub = makeBinaryOpUnion(isub);
|
|
413
|
+
var umul = makeBinaryOpUnion(imul);
|
|
414
|
+
var udiv = makeBinaryOpUnion(idiv);
|
|
415
|
+
function uneg(U) {
|
|
416
|
+
typeCheckIsUnion(U);
|
|
417
|
+
return new Union(U.intervals.slice().reverse().map(ineg));
|
|
418
|
+
}
|
|
419
|
+
var add = makeBinaryOpEither(uadd);
|
|
420
|
+
var sub = makeBinaryOpEither(usub);
|
|
421
|
+
var mul = makeBinaryOpEither(umul);
|
|
422
|
+
var div = makeBinaryOpEither(udiv);
|
|
423
|
+
|
|
424
|
+
// src/math.ts
|
|
425
|
+
function iabs(X) {
|
|
426
|
+
if (X.lo >= 0) return new Union([X]);
|
|
427
|
+
if (X.hi <= 0) return new Union([ineg(X)]);
|
|
428
|
+
return new Union([new Interval(0, Math.max(-X.lo, X.hi))]);
|
|
429
|
+
}
|
|
430
|
+
var uabs = makeUnaryOpUnion(iabs);
|
|
431
|
+
var abs = makeUnaryOpEither(uabs);
|
|
432
|
+
function imin(X, Y) {
|
|
433
|
+
return new Union([new Interval(Math.min(X.lo, Y.lo), Math.min(X.hi, Y.hi))]);
|
|
434
|
+
}
|
|
435
|
+
var umin = makeBinaryOpUnion(imin);
|
|
436
|
+
var min = makeBinaryOpEither(umin);
|
|
437
|
+
function imax(X, Y) {
|
|
438
|
+
return new Union([new Interval(Math.max(X.lo, Y.lo), Math.max(X.hi, Y.hi))]);
|
|
439
|
+
}
|
|
440
|
+
var umax = makeBinaryOpUnion(imax);
|
|
441
|
+
var max = makeBinaryOpEither(umax);
|
|
442
|
+
function leftExp(x) {
|
|
443
|
+
if (x === -Infinity) return 0;
|
|
444
|
+
if (x === 0) return 1;
|
|
445
|
+
return prev(Math.exp(x));
|
|
446
|
+
}
|
|
447
|
+
function rightExp(x) {
|
|
448
|
+
if (x === 0) return 1;
|
|
449
|
+
return next(Math.exp(x));
|
|
450
|
+
}
|
|
451
|
+
function iexp(X) {
|
|
452
|
+
typeCheckIsInterval(X);
|
|
453
|
+
return new Interval(leftExp(X.lo), rightExp(X.hi));
|
|
454
|
+
}
|
|
455
|
+
function uexp(U) {
|
|
456
|
+
typeCheckIsUnion(U);
|
|
457
|
+
return new Union(U.intervals.map(iexp));
|
|
458
|
+
}
|
|
459
|
+
var exp = makeUnaryOpEither(uexp);
|
|
460
|
+
function leftLog(x) {
|
|
461
|
+
if (x === 0) return -Infinity;
|
|
462
|
+
if (x === 1) return 0;
|
|
463
|
+
return prev(Math.log(x));
|
|
464
|
+
}
|
|
465
|
+
function rightLog(x) {
|
|
466
|
+
if (x === 0) return -Infinity;
|
|
467
|
+
if (x === 1) return 0;
|
|
468
|
+
return next(Math.log(x));
|
|
469
|
+
}
|
|
470
|
+
function ilog(X) {
|
|
471
|
+
typeCheckIsInterval(X);
|
|
472
|
+
if (X.hi <= 0) return new Union([]);
|
|
473
|
+
if (X.lo <= 0) {
|
|
474
|
+
return new Union([new Interval(-Infinity, rightLog(X.hi))]);
|
|
475
|
+
} else {
|
|
476
|
+
return new Union([new Interval(leftLog(Math.max(0, X.lo)), rightLog(X.hi))]);
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
function ulog(U) {
|
|
480
|
+
typeCheckIsUnion(U);
|
|
481
|
+
return new Union(
|
|
482
|
+
U.intervals.map((i) => ilog(i).intervals).filter((il) => il.length > 0).flat()
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
var log = makeUnaryOpEither(ulog);
|
|
486
|
+
function leftPowInt(base, exponent) {
|
|
487
|
+
if (base === 0) return 0;
|
|
488
|
+
if (exponent === 0) return 1;
|
|
489
|
+
return prev(Math.pow(base, exponent));
|
|
490
|
+
}
|
|
491
|
+
function rightPowInt(base, exponent) {
|
|
492
|
+
if (base === 0) return 0;
|
|
493
|
+
if (exponent === 0) return 1;
|
|
494
|
+
return next(Math.pow(base, exponent));
|
|
495
|
+
}
|
|
496
|
+
function ipowInt(X, n) {
|
|
497
|
+
typeCheckIsInterval(X);
|
|
498
|
+
if (!Number.isInteger(n)) {
|
|
499
|
+
throw Error(`ipowint: exponent must be an integer, got ${n}`);
|
|
500
|
+
}
|
|
501
|
+
if (n < 0) return idiv(new Interval(1, 1), ipowInt(X, -n).intervals[0]);
|
|
502
|
+
if (n === 0) return new Union([X]);
|
|
503
|
+
if (X.lo > 0 || n % 2 !== 0) {
|
|
504
|
+
return new Union([new Interval(leftPowInt(X.lo, n), rightPowInt(X.hi, n))]);
|
|
505
|
+
} else if (X.hi < 0) {
|
|
506
|
+
return new Union([new Interval(leftPowInt(X.hi, n), rightPowInt(X.lo, n))]);
|
|
507
|
+
} else {
|
|
508
|
+
return new Union([new Interval(0, rightPowInt(Math.abs(X.lo), n))]);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
function upowInt(U, n) {
|
|
512
|
+
typeCheckIsUnion(U);
|
|
513
|
+
return union(U.intervals.map((i) => ipowInt(i, n).intervals).flat());
|
|
514
|
+
}
|
|
515
|
+
function powInt(A, n) {
|
|
516
|
+
typeCheckIsIntervalOrUnion(A);
|
|
517
|
+
return upowInt(toUnion(A), n);
|
|
518
|
+
}
|
|
519
|
+
function ipow(X, Y) {
|
|
520
|
+
if (X.hi <= 0) return new Union([]);
|
|
521
|
+
return uexp(umul(new Union([Y]), ilog(X)));
|
|
522
|
+
}
|
|
523
|
+
var upow = makeBinaryOpUnion(ipow);
|
|
524
|
+
var pow = makeBinaryOpEither(upow);
|
|
525
|
+
function leftSqrt(x) {
|
|
526
|
+
if (x === 0) return 0;
|
|
527
|
+
if (x === 1) return 1;
|
|
528
|
+
return prev(Math.sqrt(x));
|
|
529
|
+
}
|
|
530
|
+
function rightSqrt(x) {
|
|
531
|
+
if (x === 0) return 0;
|
|
532
|
+
if (x === 1) return 1;
|
|
533
|
+
return next(Math.sqrt(x));
|
|
534
|
+
}
|
|
535
|
+
function isqrt(X) {
|
|
536
|
+
if (X.hi < 0) return EMPTY;
|
|
537
|
+
return new Union([new Interval(leftSqrt(Math.max(0, X.lo)), rightSqrt(X.hi))]);
|
|
538
|
+
}
|
|
539
|
+
function usqrt(U) {
|
|
540
|
+
typeCheckIsUnion(U);
|
|
541
|
+
return new Union(
|
|
542
|
+
U.intervals.map((i) => isqrt(i).intervals).filter((il) => il.length > 0).flat()
|
|
543
|
+
);
|
|
544
|
+
}
|
|
545
|
+
var sqrt = makeUnaryOpEither(usqrt);
|
|
546
|
+
|
|
547
|
+
// src/trigonometry.ts
|
|
548
|
+
var pi = Math.PI;
|
|
549
|
+
var halfpi = Math.PI / 2;
|
|
550
|
+
var tau = 2 * Math.PI;
|
|
551
|
+
function wrapAngle(angle) {
|
|
552
|
+
return (angle % tau + tau) % tau;
|
|
553
|
+
}
|
|
554
|
+
function piMultiples(X) {
|
|
555
|
+
const lower = Math.ceil(X.lo / Math.PI);
|
|
556
|
+
const upper = Math.floor(X.hi / Math.PI);
|
|
557
|
+
if (lower > upper) {
|
|
558
|
+
return [];
|
|
559
|
+
}
|
|
560
|
+
const result = [];
|
|
561
|
+
for (let n = lower; n <= upper; n++) {
|
|
562
|
+
result.push(n);
|
|
563
|
+
}
|
|
564
|
+
return result;
|
|
565
|
+
}
|
|
566
|
+
function halfPiMultiples(X) {
|
|
567
|
+
const lower = Math.ceil((X.lo - Math.PI / 2) / Math.PI);
|
|
568
|
+
const upper = Math.floor((X.hi - Math.PI / 2) / Math.PI);
|
|
569
|
+
if (lower > upper) {
|
|
570
|
+
return [];
|
|
571
|
+
}
|
|
572
|
+
const result = [];
|
|
573
|
+
for (let n = lower; n <= upper; n++) {
|
|
574
|
+
result.push(n);
|
|
575
|
+
}
|
|
576
|
+
return result;
|
|
577
|
+
}
|
|
578
|
+
function leftCos(x) {
|
|
579
|
+
if (x === 0) return 1;
|
|
580
|
+
return Math.max(-1, Math.min(1, prev(Math.cos(x))));
|
|
581
|
+
}
|
|
582
|
+
function rightCos(x) {
|
|
583
|
+
if (x === 0) return 1;
|
|
584
|
+
return Math.max(-1, Math.min(1, next(Math.cos(x))));
|
|
585
|
+
}
|
|
586
|
+
function icos(X) {
|
|
587
|
+
typeCheckIsInterval(X);
|
|
588
|
+
const width = X.hi - X.lo;
|
|
589
|
+
if (width >= 2 * Math.PI) return new Union([new Interval(-1, 1)]);
|
|
590
|
+
const candidates = [X.lo, X.hi, ...piMultiples(X).map((x) => x * pi)];
|
|
591
|
+
const left = candidates.map(leftCos);
|
|
592
|
+
const right = candidates.map(rightCos);
|
|
593
|
+
return new Union([new Interval(Math.min(...left), Math.max(...right))]);
|
|
594
|
+
}
|
|
595
|
+
var ucos = makeUnaryOpUnion(icos);
|
|
596
|
+
var cos = makeUnaryOpEither(ucos);
|
|
597
|
+
function leftSin(x) {
|
|
598
|
+
if (x === 0) return 0;
|
|
599
|
+
return Math.max(-1, Math.min(1, prev(Math.sin(x))));
|
|
600
|
+
}
|
|
601
|
+
function rightSin(x) {
|
|
602
|
+
if (x === 0) return 0;
|
|
603
|
+
return Math.max(-1, Math.min(1, next(Math.sin(x))));
|
|
604
|
+
}
|
|
605
|
+
function isin(X) {
|
|
606
|
+
typeCheckIsInterval(X);
|
|
607
|
+
const width = X.hi - X.lo;
|
|
608
|
+
if (width >= 2 * Math.PI) return new Union([new Interval(-1, 1)]);
|
|
609
|
+
const candidates = [X.lo, X.hi, ...halfPiMultiples(X).map((x) => halfpi + x * pi)];
|
|
610
|
+
const left = candidates.map(leftSin);
|
|
611
|
+
const right = candidates.map(rightSin);
|
|
612
|
+
return new Union([new Interval(Math.min(...left), Math.max(...right))]);
|
|
613
|
+
}
|
|
614
|
+
var usin = makeUnaryOpUnion(isin);
|
|
615
|
+
var sin = makeUnaryOpEither(usin);
|
|
616
|
+
function utan(U) {
|
|
617
|
+
return udiv(usin(U), ucos(U));
|
|
618
|
+
}
|
|
619
|
+
var tan = makeUnaryOpEither(utan);
|
|
5
620
|
export {
|
|
6
|
-
|
|
621
|
+
EMPTY,
|
|
622
|
+
FULL,
|
|
623
|
+
Interval,
|
|
624
|
+
Union,
|
|
625
|
+
abs,
|
|
626
|
+
add,
|
|
627
|
+
bounded,
|
|
628
|
+
cos,
|
|
629
|
+
defaultNumbers,
|
|
630
|
+
disjoint,
|
|
631
|
+
div,
|
|
632
|
+
exp,
|
|
633
|
+
halfPiMultiples,
|
|
634
|
+
iabs,
|
|
635
|
+
iadd,
|
|
636
|
+
icos,
|
|
637
|
+
idisjoint,
|
|
638
|
+
idiv,
|
|
639
|
+
iexp,
|
|
640
|
+
ilog,
|
|
641
|
+
imax,
|
|
642
|
+
imin,
|
|
643
|
+
imul,
|
|
644
|
+
ineg,
|
|
645
|
+
inter,
|
|
646
|
+
interval,
|
|
647
|
+
intervalType,
|
|
648
|
+
ioverlap,
|
|
649
|
+
ipow,
|
|
650
|
+
ipowInt,
|
|
651
|
+
isIntervalOrUnion,
|
|
652
|
+
isin,
|
|
653
|
+
isqrt,
|
|
654
|
+
isub,
|
|
655
|
+
iudisjoint,
|
|
656
|
+
iuoverlap,
|
|
657
|
+
leftMul,
|
|
658
|
+
log,
|
|
659
|
+
makeBinaryOpEither,
|
|
660
|
+
makeBinaryOpUnion,
|
|
661
|
+
makeUnaryOpEither,
|
|
662
|
+
makeUnaryOpUnion,
|
|
663
|
+
max,
|
|
664
|
+
min,
|
|
665
|
+
mul,
|
|
666
|
+
next,
|
|
667
|
+
overlap,
|
|
668
|
+
piMultiples,
|
|
669
|
+
pow,
|
|
670
|
+
powInt,
|
|
671
|
+
prev,
|
|
672
|
+
rightMul,
|
|
673
|
+
sin,
|
|
674
|
+
sqrt,
|
|
675
|
+
sub,
|
|
676
|
+
tan,
|
|
677
|
+
toUnion,
|
|
678
|
+
typeCheckIsInterval,
|
|
679
|
+
typeCheckIsIntervalOrUnion,
|
|
680
|
+
typeCheckIsUnion,
|
|
681
|
+
uabs,
|
|
682
|
+
uadd,
|
|
683
|
+
ucos,
|
|
684
|
+
udiv,
|
|
685
|
+
uexp,
|
|
686
|
+
ulog,
|
|
687
|
+
umax,
|
|
688
|
+
umin,
|
|
689
|
+
umul,
|
|
690
|
+
uneg,
|
|
691
|
+
union,
|
|
692
|
+
upow,
|
|
693
|
+
upowInt,
|
|
694
|
+
usin,
|
|
695
|
+
usqrt,
|
|
696
|
+
usub,
|
|
697
|
+
utan,
|
|
698
|
+
wrapAngle
|
|
7
699
|
};
|
|
8
700
|
//# sourceMappingURL=index.js.map
|