@mark1russell7/interval 0.0.0-stage → 0.1.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 +21 -0
- package/README.md +38 -2
- package/build/index.d.ts +419 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +828 -0
- package/build/index.js.map +1 -0
- package/build/rolldown-runtime-D3KUYPsf.js +12 -0
- package/package.json +64 -3
- package/src/ball.ts +377 -0
- package/src/index.ts +13 -0
- package/src/interval.ts +376 -0
- package/src/rounding.ts +70 -0
package/src/interval.ts
ADDED
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Intervals of real numbers with float bounds. Each operation rounds outward, so the exact result of the operation on
|
|
3
|
+
* any reals inside the inputs is inside the output. An infinite bound means that the interval has no bound on that side.
|
|
4
|
+
*
|
|
5
|
+
* `null` is the empty interval: no point of the inputs gives a real value.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** A closed interval `[low, high]` of reals, or `null` for the empty interval. */
|
|
11
|
+
export type Interval = readonly [number, number] | null;
|
|
12
|
+
|
|
13
|
+
/** The interval that contains every real. */
|
|
14
|
+
export const WHOLE: readonly [number, number] = [-Infinity, Infinity];
|
|
15
|
+
|
|
16
|
+
const view = new DataView(new ArrayBuffer(8));
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* This function gives the next float above a value.
|
|
20
|
+
*
|
|
21
|
+
* @param x - The value.
|
|
22
|
+
* @returns The smallest float that is larger than `x`. `Infinity` stays `Infinity`, and `NaN` stays `NaN`.
|
|
23
|
+
*/
|
|
24
|
+
export function nextUp(x: number): number {
|
|
25
|
+
if (Number.isNaN(x) || x === Infinity) return x;
|
|
26
|
+
if (x === 0) return Number.MIN_VALUE;
|
|
27
|
+
view.setFloat64(0, x);
|
|
28
|
+
const bits = view.getBigUint64(0);
|
|
29
|
+
view.setBigUint64(0, x > 0 ? bits + 1n : bits - 1n);
|
|
30
|
+
return view.getFloat64(0);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* This function gives the next float below a value.
|
|
35
|
+
*
|
|
36
|
+
* @param x - The value.
|
|
37
|
+
* @returns The largest float that is smaller than `x`. `-Infinity` stays `-Infinity`, and `NaN` stays `NaN`.
|
|
38
|
+
*/
|
|
39
|
+
export function nextDown(x: number): number {
|
|
40
|
+
return -nextUp(-x);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* This function gives the exact error of a float sum. It uses the error term of `TwoSum` (Knuth).
|
|
45
|
+
*
|
|
46
|
+
* @param a - The first value.
|
|
47
|
+
* @param b - The second value.
|
|
48
|
+
* @param s - The float sum `a + b`.
|
|
49
|
+
* @returns The float `e` with `a + b = s + e` exactly, or `NaN` when `s` is not finite and the term is not reliable.
|
|
50
|
+
*/
|
|
51
|
+
export function sumError(a: number, b: number, s: number): number {
|
|
52
|
+
if (!Number.isFinite(s)) return Number.NaN;
|
|
53
|
+
const bb = s - a;
|
|
54
|
+
return a - (s - bb) + (b - bb);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const SPLIT = 134217729; // 2^27 + 1, the constant of the split of Veltkamp.
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* This function gives the exact error of a float product. It uses the error term of `TwoProduct` (Dekker). Near
|
|
61
|
+
* overflow or underflow the term is not reliable, so the function then gives `NaN`.
|
|
62
|
+
*
|
|
63
|
+
* @param a - The first value.
|
|
64
|
+
* @param b - The second value.
|
|
65
|
+
* @param p - The float product `a * b`.
|
|
66
|
+
* @returns The float `e` with `a * b = p + e` exactly, or `NaN` when the term is not reliable.
|
|
67
|
+
*/
|
|
68
|
+
export function productError(a: number, b: number, p: number): number {
|
|
69
|
+
if (a === 0 || b === 0) return 0;
|
|
70
|
+
const m = Math.abs(p);
|
|
71
|
+
if (!(m > 2 ** -900 && m < 2 ** 990) || Math.abs(a) > 2 ** 990 || Math.abs(b) > 2 ** 990) return Number.NaN;
|
|
72
|
+
const ca = SPLIT * a;
|
|
73
|
+
const ah = ca - (ca - a);
|
|
74
|
+
const al = a - ah;
|
|
75
|
+
const cb = SPLIT * b;
|
|
76
|
+
const bh = cb - (cb - b);
|
|
77
|
+
const bl = b - bh;
|
|
78
|
+
return al * bl - (p - ah * bh - al * bh - ah * bl);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* This function gives a lower bound of `v + e` for a float `v` and its exact error `e`. An exact `v` is the bound.
|
|
83
|
+
* Else the bound is one float lower. Round to nearest puts `v + e` within half a step of `v`, so one step is enough.
|
|
84
|
+
* An error of `NaN` is not known, and it gives the step. The sign of `e` is not used, so that each bound of A1 stays
|
|
85
|
+
* the same.
|
|
86
|
+
*/
|
|
87
|
+
const below = (v: number, e: number): number => (e === 0 ? v : nextDown(v));
|
|
88
|
+
|
|
89
|
+
/** This function gives an upper bound of `v + e`, with the rule of `below`. */
|
|
90
|
+
const above = (v: number, e: number): number => (e === 0 ? v : nextUp(v));
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* This function gives the sum of two intervals, rounded outward.
|
|
94
|
+
*
|
|
95
|
+
* @param x - The first interval.
|
|
96
|
+
* @param y - The second interval.
|
|
97
|
+
* @returns An interval that contains each sum of a point of `x` and a point of `y`.
|
|
98
|
+
*/
|
|
99
|
+
export function add(x: Interval, y: Interval): Interval {
|
|
100
|
+
if (x === null || y === null) return null;
|
|
101
|
+
const lo = x[0] + y[0];
|
|
102
|
+
const hi = x[1] + y[1];
|
|
103
|
+
return [below(lo, sumError(x[0], y[0], lo)), above(hi, sumError(x[1], y[1], hi))];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* This function gives the negation of an interval. It is exact.
|
|
108
|
+
*
|
|
109
|
+
* @param x - The interval.
|
|
110
|
+
* @returns The interval of the negated points.
|
|
111
|
+
*/
|
|
112
|
+
export function neg(x: Interval): Interval {
|
|
113
|
+
return x === null ? null : [-x[1], -x[0]];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* This function gives the difference of two intervals, rounded outward.
|
|
118
|
+
*
|
|
119
|
+
* @param x - The first interval.
|
|
120
|
+
* @param y - The interval to subtract.
|
|
121
|
+
* @returns An interval that contains each difference of a point of `x` and a point of `y`.
|
|
122
|
+
*/
|
|
123
|
+
export function sub(x: Interval, y: Interval): Interval {
|
|
124
|
+
return add(x, neg(y));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* This function gives one float product for an interval bound. A zero times an infinite bound is zero, because the
|
|
129
|
+
* infinite bound stands for large reals, not for infinity.
|
|
130
|
+
*/
|
|
131
|
+
function boundProduct(a: number, b: number): number {
|
|
132
|
+
return a === 0 || b === 0 ? 0 : a * b;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* This function gives the product of two intervals, rounded outward.
|
|
137
|
+
*
|
|
138
|
+
* @param x - The first interval.
|
|
139
|
+
* @param y - The second interval.
|
|
140
|
+
* @returns An interval that contains each product of a point of `x` and a point of `y`.
|
|
141
|
+
*/
|
|
142
|
+
export function mul(x: Interval, y: Interval): Interval {
|
|
143
|
+
if (x === null || y === null) return null;
|
|
144
|
+
let lo = Infinity;
|
|
145
|
+
let hi = -Infinity;
|
|
146
|
+
for (const a of x) {
|
|
147
|
+
for (const b of y) {
|
|
148
|
+
const p = boundProduct(a, b);
|
|
149
|
+
const e = productError(a, b, p);
|
|
150
|
+
lo = Math.min(lo, below(p, e));
|
|
151
|
+
hi = Math.max(hi, above(p, e));
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return [lo, hi];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* This function tells if a float quotient is exact: the product `q * b` is exact and is `a`.
|
|
159
|
+
*
|
|
160
|
+
* @param a - The dividend.
|
|
161
|
+
* @param b - The divisor.
|
|
162
|
+
* @param q - The float quotient `a / b`.
|
|
163
|
+
* @returns `true` when `q` is the exact quotient.
|
|
164
|
+
*/
|
|
165
|
+
export function quotientIsExact(a: number, b: number, q: number): boolean {
|
|
166
|
+
const p = q * b;
|
|
167
|
+
return p === a && productError(q, b, p) === 0;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* This function gives the quotient of two intervals, rounded outward. A divisor interval that contains zero gives the
|
|
172
|
+
* whole line, and the point zero gives the empty interval. A quotient of two finite bounds moves one step outward on
|
|
173
|
+
* each side when it is not exact. A quotient with an infinite bound is `0` or infinite, and it is a bound as it is.
|
|
174
|
+
*
|
|
175
|
+
* @param x - The dividend.
|
|
176
|
+
* @param y - The divisor.
|
|
177
|
+
* @returns An interval that contains each quotient of a point of `x` and a nonzero point of `y`.
|
|
178
|
+
*/
|
|
179
|
+
export function div(x: Interval, y: Interval): Interval {
|
|
180
|
+
if (x === null || y === null) return null;
|
|
181
|
+
if (y[0] === 0 && y[1] === 0) return null;
|
|
182
|
+
if (y[0] <= 0 && y[1] >= 0) return WHOLE;
|
|
183
|
+
let lo = Infinity;
|
|
184
|
+
let hi = -Infinity;
|
|
185
|
+
for (const a of x) {
|
|
186
|
+
for (const b of y) {
|
|
187
|
+
if (Number.isFinite(a) && Number.isFinite(b)) {
|
|
188
|
+
const q = a / b;
|
|
189
|
+
const exact = quotientIsExact(a, b, q);
|
|
190
|
+
lo = Math.min(lo, exact ? q : nextDown(q));
|
|
191
|
+
hi = Math.max(hi, exact ? q : nextUp(q));
|
|
192
|
+
} else {
|
|
193
|
+
const q = Number.isFinite(b) ? a / b : Number.isFinite(a) ? 0 : (a > 0) === (b > 0) ? Infinity : -Infinity;
|
|
194
|
+
lo = Math.min(lo, q);
|
|
195
|
+
hi = Math.max(hi, q);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return [lo, hi];
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* This function gives the square of an interval, rounded outward. It is tighter than `mul(x, x)` for an interval
|
|
204
|
+
* that contains zero.
|
|
205
|
+
*/
|
|
206
|
+
function square(x: readonly [number, number]): Interval {
|
|
207
|
+
const [a, b] = x;
|
|
208
|
+
if (a >= 0) return mul(x, x);
|
|
209
|
+
if (b <= 0) return mul(neg(x), neg(x));
|
|
210
|
+
const m = Math.max(-a, b);
|
|
211
|
+
const top = mul([m, m], [m, m]);
|
|
212
|
+
return top === null ? null : [0, top[1]];
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* This function gives an interval to a whole power, rounded outward. The power zero gives the point one, also for a
|
|
217
|
+
* base of zero. A negative power is the reciprocal of the positive power.
|
|
218
|
+
*
|
|
219
|
+
* @param x - The base.
|
|
220
|
+
* @param n - The exponent. It must be a whole number.
|
|
221
|
+
* @returns An interval that contains each power of a point of `x`.
|
|
222
|
+
*/
|
|
223
|
+
export function pow(x: Interval, n: number): Interval {
|
|
224
|
+
if (x === null) return null;
|
|
225
|
+
if (!Number.isInteger(n)) throw new RangeError(`pow needs a whole exponent, not ${n}.`);
|
|
226
|
+
if (n < 0) return div([1, 1], pow(x, -n));
|
|
227
|
+
let result: Interval = [1, 1];
|
|
228
|
+
let base: Interval = x;
|
|
229
|
+
for (let k = n; k > 0; k = Math.floor(k / 2)) {
|
|
230
|
+
if (k % 2 === 1) result = mul(result, base);
|
|
231
|
+
if (k > 1 && base !== null) base = square(base);
|
|
232
|
+
}
|
|
233
|
+
return result;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** This function tells if `r * r` is exactly `a`. */
|
|
237
|
+
function isSquareOf(r: number, a: number): boolean {
|
|
238
|
+
const p = r * r;
|
|
239
|
+
return p === a && productError(r, r, p) === 0;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** This function gives the smallest power of the interval `[z, z]`, rounded downward. */
|
|
243
|
+
function powLow(z: number, k: number): number {
|
|
244
|
+
const p = pow([z, z], k);
|
|
245
|
+
return p === null ? Number.NaN : p[0];
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** This function gives the largest power of the interval `[z, z]`, rounded upward. */
|
|
249
|
+
function powHigh(z: number, k: number): number {
|
|
250
|
+
const p = pow([z, z], k);
|
|
251
|
+
return p === null ? Number.NaN : p[1];
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const rootOf = (m: number, k: number): number => (k === 3 ? Math.cbrt(m) : Math.pow(m, 1 / k));
|
|
255
|
+
|
|
256
|
+
/** The most steps of the search of `rootUp` and `rootDown`. After them, each function gives its trivial bound. */
|
|
257
|
+
const STEPS = 64;
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* This function gives a float `z >= 0` with `z^k >= m`, for `m >= 0`. The check uses the outward powers of `pow`.
|
|
261
|
+
* Near overflow the check cannot pass, and the function gives `Infinity`.
|
|
262
|
+
*/
|
|
263
|
+
function rootUp(m: number, k: number): number {
|
|
264
|
+
let z = rootOf(m, k);
|
|
265
|
+
for (let i = 0; z < Infinity && i < STEPS; i++) {
|
|
266
|
+
if (powLow(z, k) >= m) return z;
|
|
267
|
+
z = i < 8 ? nextUp(z) : z * (1 + 2 ** (i - 60));
|
|
268
|
+
}
|
|
269
|
+
return Infinity;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** This function gives a float `z >= 0` with `z^k <= m`, for `m >= 0`. After `STEPS` steps it gives `0`. */
|
|
273
|
+
function rootDown(m: number, k: number): number {
|
|
274
|
+
let z = rootOf(m, k);
|
|
275
|
+
if (m === Infinity) return z;
|
|
276
|
+
for (let i = 0; z > 0 && i < STEPS; i++) {
|
|
277
|
+
if (powHigh(z, k) <= m) return z;
|
|
278
|
+
z = i < 8 ? nextDown(z) : z * (1 - 2 ** (i - 60));
|
|
279
|
+
}
|
|
280
|
+
return 0;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* This function gives the root of an interval, rounded outward. The square root uses `Math.sqrt`, which is correctly
|
|
285
|
+
* rounded, and one step on each bound. A larger index starts from `Math.pow` and moves each bound until the outward
|
|
286
|
+
* power of `pow` shows the side of the bound. The root is monotone, so the bounds of the ends are the bounds. An odd
|
|
287
|
+
* index takes negative points as `-root(-x)`. An even index ignores negative points.
|
|
288
|
+
*
|
|
289
|
+
* @param x - The radicand.
|
|
290
|
+
* @param k - The index of the root. It must be a whole number of 2 or more.
|
|
291
|
+
* @returns An interval that contains the real root of each point of `x` that has one.
|
|
292
|
+
*/
|
|
293
|
+
export function root(x: Interval, k: number): Interval {
|
|
294
|
+
if (x === null) return null;
|
|
295
|
+
if (!Number.isInteger(k) || k < 2) throw new RangeError(`root needs a whole index of 2 or more, not ${k}.`);
|
|
296
|
+
let [a, b] = x;
|
|
297
|
+
if (k % 2 === 0) {
|
|
298
|
+
if (b < 0) return null;
|
|
299
|
+
a = Math.max(a, 0);
|
|
300
|
+
}
|
|
301
|
+
if (k === 2) {
|
|
302
|
+
const lo = Math.sqrt(a);
|
|
303
|
+
const hi = Math.sqrt(b);
|
|
304
|
+
return [isSquareOf(lo, a) ? lo : Math.max(0, nextDown(lo)), isSquareOf(hi, b) ? hi : nextUp(hi)];
|
|
305
|
+
}
|
|
306
|
+
return [a < 0 ? -rootUp(-a, k) : rootDown(a, k), b < 0 ? -rootDown(-b, k) : rootUp(b, k)];
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* This function gives the interval of the smallest of the points. It is exact.
|
|
311
|
+
*
|
|
312
|
+
* @param x - The first interval.
|
|
313
|
+
* @param y - The second interval.
|
|
314
|
+
* @returns The interval of `min(p, q)` for `p` in `x` and `q` in `y`.
|
|
315
|
+
*/
|
|
316
|
+
export function min(x: Interval, y: Interval): Interval {
|
|
317
|
+
return x === null || y === null ? null : [Math.min(x[0], y[0]), Math.min(x[1], y[1])];
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* This function gives the interval of the largest of the points. It is exact.
|
|
322
|
+
*
|
|
323
|
+
* @param x - The first interval.
|
|
324
|
+
* @param y - The second interval.
|
|
325
|
+
* @returns The interval of `max(p, q)` for `p` in `x` and `q` in `y`.
|
|
326
|
+
*/
|
|
327
|
+
export function max(x: Interval, y: Interval): Interval {
|
|
328
|
+
return x === null || y === null ? null : [Math.max(x[0], y[0]), Math.max(x[1], y[1])];
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** The interval of a truth value: `1` is true and `0` is false. */
|
|
332
|
+
const TRUE: Interval = [1, 1];
|
|
333
|
+
const FALSE: Interval = [0, 0];
|
|
334
|
+
const UNKNOWN: Interval = [0, 1];
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* This function compares two intervals with `<`. The result is the interval of the truth value, with `1` for true.
|
|
338
|
+
*
|
|
339
|
+
* @param x - The left side.
|
|
340
|
+
* @param y - The right side.
|
|
341
|
+
* @returns `[1, 1]` when `p < q` for each pair of points, `[0, 0]` when for none, and `[0, 1]` when it is not known.
|
|
342
|
+
*/
|
|
343
|
+
export function lt(x: Interval, y: Interval): Interval {
|
|
344
|
+
if (x === null || y === null) return null;
|
|
345
|
+
if (x[1] < y[0]) return TRUE;
|
|
346
|
+
if (x[0] >= y[1]) return FALSE;
|
|
347
|
+
return UNKNOWN;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* This function compares two intervals with `<=`. The result is the interval of the truth value, with `1` for true.
|
|
352
|
+
*
|
|
353
|
+
* @param x - The left side.
|
|
354
|
+
* @param y - The right side.
|
|
355
|
+
* @returns `[1, 1]` when `p <= q` for each pair of points, `[0, 0]` when for none, and `[0, 1]` when it is not known.
|
|
356
|
+
*/
|
|
357
|
+
export function le(x: Interval, y: Interval): Interval {
|
|
358
|
+
if (x === null || y === null) return null;
|
|
359
|
+
if (x[1] <= y[0]) return TRUE;
|
|
360
|
+
if (x[0] > y[1]) return FALSE;
|
|
361
|
+
return UNKNOWN;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* This function compares two intervals with `=`. The result is the interval of the truth value, with `1` for true.
|
|
366
|
+
*
|
|
367
|
+
* @param x - The left side.
|
|
368
|
+
* @param y - The right side.
|
|
369
|
+
* @returns `[1, 1]` when both are the same point, `[0, 0]` when they do not meet, and `[0, 1]` when it is not known.
|
|
370
|
+
*/
|
|
371
|
+
export function eq(x: Interval, y: Interval): Interval {
|
|
372
|
+
if (x === null || y === null) return null;
|
|
373
|
+
if (x[0] === x[1] && y[0] === y[1] && x[0] === y[0]) return TRUE;
|
|
374
|
+
if (x[1] < y[0] || y[1] < x[0]) return FALSE;
|
|
375
|
+
return UNKNOWN;
|
|
376
|
+
}
|
package/src/rounding.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Directed rounding of nonnegative floats, from the exact error terms of `interval.ts`. Each function gives the
|
|
3
|
+
* correctly rounded result when it is exact, and else the result one float step in the stated direction.
|
|
4
|
+
*
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
*/
|
|
7
|
+
import { nextDown, nextUp, productError, quotientIsExact, sumError } from "./interval.ts";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* This function gives the sum of two nonnegative floats, rounded upward.
|
|
11
|
+
*
|
|
12
|
+
* @param x - The first value, 0 or more.
|
|
13
|
+
* @param y - The second value, 0 or more.
|
|
14
|
+
* @returns A float that is at least `x + y`, and at most one step above it.
|
|
15
|
+
*/
|
|
16
|
+
export function addUp(x: number, y: number): number {
|
|
17
|
+
const s = x + y;
|
|
18
|
+
return sumError(x, y, s) <= 0 ? s : nextUp(s);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* This function gives the product of two nonnegative floats, rounded upward.
|
|
23
|
+
*
|
|
24
|
+
* @param x - The first value, 0 or more.
|
|
25
|
+
* @param y - The second value, 0 or more.
|
|
26
|
+
* @returns A float that is at least `x * y`, and at most one step above it.
|
|
27
|
+
*/
|
|
28
|
+
export function mulUp(x: number, y: number): number {
|
|
29
|
+
const p = x * y;
|
|
30
|
+
return productError(x, y, p) <= 0 ? p : nextUp(p);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* This function gives the quotient of two nonnegative floats, rounded upward. A correctly rounded quotient is at most
|
|
35
|
+
* one step low.
|
|
36
|
+
*
|
|
37
|
+
* @param x - The dividend, 0 or more.
|
|
38
|
+
* @param y - The divisor, above 0.
|
|
39
|
+
* @returns A float that is at least `x / y`, and at most one step above it.
|
|
40
|
+
*/
|
|
41
|
+
export function divUp(x: number, y: number): number {
|
|
42
|
+
const q = x / y;
|
|
43
|
+
return x === 0 || quotientIsExact(x, y, q) ? q : nextUp(q);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* This function gives the difference `x - y` of two nonnegative floats, rounded downward.
|
|
48
|
+
*
|
|
49
|
+
* @param x - The first value, 0 or more.
|
|
50
|
+
* @param y - The value to subtract, 0 or more.
|
|
51
|
+
* @returns A float that is at most `x - y`, and at most one step below it.
|
|
52
|
+
*/
|
|
53
|
+
export function subDown(x: number, y: number): number {
|
|
54
|
+
const s = x - y;
|
|
55
|
+
return sumError(x, -y, s) >= 0 ? s : nextDown(s);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* This function gives the float root of index `k`. The square root is correctly rounded. A larger index uses
|
|
60
|
+
* `Math.cbrt` or `Math.pow`, which are not correctly rounded. An odd root of a negative value is negative.
|
|
61
|
+
*
|
|
62
|
+
* @param r - The radicand.
|
|
63
|
+
* @param k - The index of the root, 2 or more.
|
|
64
|
+
* @returns The float root.
|
|
65
|
+
*/
|
|
66
|
+
export function floatRoot(r: number, k: number): number {
|
|
67
|
+
if (k === 2) return Math.sqrt(r);
|
|
68
|
+
const f = (m: number): number => (k === 3 ? Math.cbrt(m) : Math.pow(m, 1 / k));
|
|
69
|
+
return k % 2 === 1 && r < 0 ? -f(-r) : f(r);
|
|
70
|
+
}
|