@mark1russell7/interval 0.0.0-bootstrap.0 → 0.1.0

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.
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":["add","neg","sub","mul","div","pow","root","min","max","lt","le","eq","I.WHOLE","I.add","I.sub","I.sumError","I.productError","I.nextUp","I.quotientIsExact","I.root","I"],"sources":["../src/interval.ts","../src/rounding.ts","../src/ball.ts"],"sourcesContent":["/**\n * Intervals of real numbers with float bounds. Each operation rounds outward, so the exact result of the operation on\n * any reals inside the inputs is inside the output. An infinite bound means that the interval has no bound on that side.\n *\n * `null` is the empty interval: no point of the inputs gives a real value.\n *\n * @packageDocumentation\n */\n\n/** A closed interval `[low, high]` of reals, or `null` for the empty interval. */\nexport type Interval = readonly [number, number] | null;\n\n/** The interval that contains every real. */\nexport const WHOLE: readonly [number, number] = [-Infinity, Infinity];\n\nconst view = new DataView(new ArrayBuffer(8));\n\n/**\n * This function gives the next float above a value.\n *\n * @param x - The value.\n * @returns The smallest float that is larger than `x`. `Infinity` stays `Infinity`, and `NaN` stays `NaN`.\n */\nexport function nextUp(x: number): number {\n if (Number.isNaN(x) || x === Infinity) return x;\n if (x === 0) return Number.MIN_VALUE;\n view.setFloat64(0, x);\n const bits = view.getBigUint64(0);\n view.setBigUint64(0, x > 0 ? bits + 1n : bits - 1n);\n return view.getFloat64(0);\n}\n\n/**\n * This function gives the next float below a value.\n *\n * @param x - The value.\n * @returns The largest float that is smaller than `x`. `-Infinity` stays `-Infinity`, and `NaN` stays `NaN`.\n */\nexport function nextDown(x: number): number {\n return -nextUp(-x);\n}\n\n/**\n * This function gives the exact error of a float sum. It uses the error term of `TwoSum` (Knuth).\n *\n * @param a - The first value.\n * @param b - The second value.\n * @param s - The float sum `a + b`.\n * @returns The float `e` with `a + b = s + e` exactly, or `NaN` when `s` is not finite and the term is not reliable.\n */\nexport function sumError(a: number, b: number, s: number): number {\n if (!Number.isFinite(s)) return Number.NaN;\n const bb = s - a;\n return a - (s - bb) + (b - bb);\n}\n\nconst SPLIT = 134217729; // 2^27 + 1, the constant of the split of Veltkamp.\n\n/**\n * This function gives the exact error of a float product. It uses the error term of `TwoProduct` (Dekker). Near\n * overflow or underflow the term is not reliable, so the function then gives `NaN`.\n *\n * @param a - The first value.\n * @param b - The second value.\n * @param p - The float product `a * b`.\n * @returns The float `e` with `a * b = p + e` exactly, or `NaN` when the term is not reliable.\n */\nexport function productError(a: number, b: number, p: number): number {\n if (a === 0 || b === 0) return 0;\n const m = Math.abs(p);\n if (!(m > 2 ** -900 && m < 2 ** 990) || Math.abs(a) > 2 ** 990 || Math.abs(b) > 2 ** 990) return Number.NaN;\n const ca = SPLIT * a;\n const ah = ca - (ca - a);\n const al = a - ah;\n const cb = SPLIT * b;\n const bh = cb - (cb - b);\n const bl = b - bh;\n return al * bl - (p - ah * bh - al * bh - ah * bl);\n}\n\n/**\n * This function gives a lower bound of `v + e` for a float `v` and its exact error `e`. An exact `v` is the bound.\n * Else the bound is one float lower. Round to nearest puts `v + e` within half a step of `v`, so one step is enough.\n * 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\n * the same.\n */\nconst below = (v: number, e: number): number => (e === 0 ? v : nextDown(v));\n\n/** This function gives an upper bound of `v + e`, with the rule of `below`. */\nconst above = (v: number, e: number): number => (e === 0 ? v : nextUp(v));\n\n/**\n * This function gives the sum of two intervals, rounded outward.\n *\n * @param x - The first interval.\n * @param y - The second interval.\n * @returns An interval that contains each sum of a point of `x` and a point of `y`.\n */\nexport function add(x: Interval, y: Interval): Interval {\n if (x === null || y === null) return null;\n const lo = x[0] + y[0];\n const hi = x[1] + y[1];\n return [below(lo, sumError(x[0], y[0], lo)), above(hi, sumError(x[1], y[1], hi))];\n}\n\n/**\n * This function gives the negation of an interval. It is exact.\n *\n * @param x - The interval.\n * @returns The interval of the negated points.\n */\nexport function neg(x: Interval): Interval {\n return x === null ? null : [-x[1], -x[0]];\n}\n\n/**\n * This function gives the difference of two intervals, rounded outward.\n *\n * @param x - The first interval.\n * @param y - The interval to subtract.\n * @returns An interval that contains each difference of a point of `x` and a point of `y`.\n */\nexport function sub(x: Interval, y: Interval): Interval {\n return add(x, neg(y));\n}\n\n/**\n * This function gives one float product for an interval bound. A zero times an infinite bound is zero, because the\n * infinite bound stands for large reals, not for infinity.\n */\nfunction boundProduct(a: number, b: number): number {\n return a === 0 || b === 0 ? 0 : a * b;\n}\n\n/**\n * This function gives the product of two intervals, rounded outward.\n *\n * @param x - The first interval.\n * @param y - The second interval.\n * @returns An interval that contains each product of a point of `x` and a point of `y`.\n */\nexport function mul(x: Interval, y: Interval): Interval {\n if (x === null || y === null) return null;\n let lo = Infinity;\n let hi = -Infinity;\n for (const a of x) {\n for (const b of y) {\n const p = boundProduct(a, b);\n const e = productError(a, b, p);\n lo = Math.min(lo, below(p, e));\n hi = Math.max(hi, above(p, e));\n }\n }\n return [lo, hi];\n}\n\n/**\n * This function tells if a float quotient is exact: the product `q * b` is exact and is `a`.\n *\n * @param a - The dividend.\n * @param b - The divisor.\n * @param q - The float quotient `a / b`.\n * @returns `true` when `q` is the exact quotient.\n */\nexport function quotientIsExact(a: number, b: number, q: number): boolean {\n const p = q * b;\n return p === a && productError(q, b, p) === 0;\n}\n\n/**\n * This function gives the quotient of two intervals, rounded outward. A divisor interval that contains zero gives the\n * whole line, and the point zero gives the empty interval. A quotient of two finite bounds moves one step outward on\n * 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.\n *\n * @param x - The dividend.\n * @param y - The divisor.\n * @returns An interval that contains each quotient of a point of `x` and a nonzero point of `y`.\n */\nexport function div(x: Interval, y: Interval): Interval {\n if (x === null || y === null) return null;\n if (y[0] === 0 && y[1] === 0) return null;\n if (y[0] <= 0 && y[1] >= 0) return WHOLE;\n let lo = Infinity;\n let hi = -Infinity;\n for (const a of x) {\n for (const b of y) {\n if (Number.isFinite(a) && Number.isFinite(b)) {\n const q = a / b;\n const exact = quotientIsExact(a, b, q);\n lo = Math.min(lo, exact ? q : nextDown(q));\n hi = Math.max(hi, exact ? q : nextUp(q));\n } else {\n const q = Number.isFinite(b) ? a / b : Number.isFinite(a) ? 0 : (a > 0) === (b > 0) ? Infinity : -Infinity;\n lo = Math.min(lo, q);\n hi = Math.max(hi, q);\n }\n }\n }\n return [lo, hi];\n}\n\n/**\n * This function gives the square of an interval, rounded outward. It is tighter than `mul(x, x)` for an interval\n * that contains zero.\n */\nfunction square(x: readonly [number, number]): Interval {\n const [a, b] = x;\n if (a >= 0) return mul(x, x);\n if (b <= 0) return mul(neg(x), neg(x));\n const m = Math.max(-a, b);\n const top = mul([m, m], [m, m]);\n return top === null ? null : [0, top[1]];\n}\n\n/**\n * This function gives an interval to a whole power, rounded outward. The power zero gives the point one, also for a\n * base of zero. A negative power is the reciprocal of the positive power.\n *\n * @param x - The base.\n * @param n - The exponent. It must be a whole number.\n * @returns An interval that contains each power of a point of `x`.\n */\nexport function pow(x: Interval, n: number): Interval {\n if (x === null) return null;\n if (!Number.isInteger(n)) throw new RangeError(`pow needs a whole exponent, not ${n}.`);\n if (n < 0) return div([1, 1], pow(x, -n));\n let result: Interval = [1, 1];\n let base: Interval = x;\n for (let k = n; k > 0; k = Math.floor(k / 2)) {\n if (k % 2 === 1) result = mul(result, base);\n if (k > 1 && base !== null) base = square(base);\n }\n return result;\n}\n\n/** This function tells if `r * r` is exactly `a`. */\nfunction isSquareOf(r: number, a: number): boolean {\n const p = r * r;\n return p === a && productError(r, r, p) === 0;\n}\n\n/** This function gives the smallest power of the interval `[z, z]`, rounded downward. */\nfunction powLow(z: number, k: number): number {\n const p = pow([z, z], k);\n return p === null ? Number.NaN : p[0];\n}\n\n/** This function gives the largest power of the interval `[z, z]`, rounded upward. */\nfunction powHigh(z: number, k: number): number {\n const p = pow([z, z], k);\n return p === null ? Number.NaN : p[1];\n}\n\nconst rootOf = (m: number, k: number): number => (k === 3 ? Math.cbrt(m) : Math.pow(m, 1 / k));\n\n/** The most steps of the search of `rootUp` and `rootDown`. After them, each function gives its trivial bound. */\nconst STEPS = 64;\n\n/**\n * This function gives a float `z >= 0` with `z^k >= m`, for `m >= 0`. The check uses the outward powers of `pow`.\n * Near overflow the check cannot pass, and the function gives `Infinity`.\n */\nfunction rootUp(m: number, k: number): number {\n let z = rootOf(m, k);\n for (let i = 0; z < Infinity && i < STEPS; i++) {\n if (powLow(z, k) >= m) return z;\n z = i < 8 ? nextUp(z) : z * (1 + 2 ** (i - 60));\n }\n return Infinity;\n}\n\n/** This function gives a float `z >= 0` with `z^k <= m`, for `m >= 0`. After `STEPS` steps it gives `0`. */\nfunction rootDown(m: number, k: number): number {\n let z = rootOf(m, k);\n if (m === Infinity) return z;\n for (let i = 0; z > 0 && i < STEPS; i++) {\n if (powHigh(z, k) <= m) return z;\n z = i < 8 ? nextDown(z) : z * (1 - 2 ** (i - 60));\n }\n return 0;\n}\n\n/**\n * This function gives the root of an interval, rounded outward. The square root uses `Math.sqrt`, which is correctly\n * rounded, and one step on each bound. A larger index starts from `Math.pow` and moves each bound until the outward\n * power of `pow` shows the side of the bound. The root is monotone, so the bounds of the ends are the bounds. An odd\n * index takes negative points as `-root(-x)`. An even index ignores negative points.\n *\n * @param x - The radicand.\n * @param k - The index of the root. It must be a whole number of 2 or more.\n * @returns An interval that contains the real root of each point of `x` that has one.\n */\nexport function root(x: Interval, k: number): Interval {\n if (x === null) return null;\n if (!Number.isInteger(k) || k < 2) throw new RangeError(`root needs a whole index of 2 or more, not ${k}.`);\n let [a, b] = x;\n if (k % 2 === 0) {\n if (b < 0) return null;\n a = Math.max(a, 0);\n }\n if (k === 2) {\n const lo = Math.sqrt(a);\n const hi = Math.sqrt(b);\n return [isSquareOf(lo, a) ? lo : Math.max(0, nextDown(lo)), isSquareOf(hi, b) ? hi : nextUp(hi)];\n }\n return [a < 0 ? -rootUp(-a, k) : rootDown(a, k), b < 0 ? -rootDown(-b, k) : rootUp(b, k)];\n}\n\n/**\n * This function gives the interval of the smallest of the points. It is exact.\n *\n * @param x - The first interval.\n * @param y - The second interval.\n * @returns The interval of `min(p, q)` for `p` in `x` and `q` in `y`.\n */\nexport function min(x: Interval, y: Interval): Interval {\n return x === null || y === null ? null : [Math.min(x[0], y[0]), Math.min(x[1], y[1])];\n}\n\n/**\n * This function gives the interval of the largest of the points. It is exact.\n *\n * @param x - The first interval.\n * @param y - The second interval.\n * @returns The interval of `max(p, q)` for `p` in `x` and `q` in `y`.\n */\nexport function max(x: Interval, y: Interval): Interval {\n return x === null || y === null ? null : [Math.max(x[0], y[0]), Math.max(x[1], y[1])];\n}\n\n/** The interval of a truth value: `1` is true and `0` is false. */\nconst TRUE: Interval = [1, 1];\nconst FALSE: Interval = [0, 0];\nconst UNKNOWN: Interval = [0, 1];\n\n/**\n * This function compares two intervals with `<`. The result is the interval of the truth value, with `1` for true.\n *\n * @param x - The left side.\n * @param y - The right side.\n * @returns `[1, 1]` when `p < q` for each pair of points, `[0, 0]` when for none, and `[0, 1]` when it is not known.\n */\nexport function lt(x: Interval, y: Interval): Interval {\n if (x === null || y === null) return null;\n if (x[1] < y[0]) return TRUE;\n if (x[0] >= y[1]) return FALSE;\n return UNKNOWN;\n}\n\n/**\n * This function compares two intervals with `<=`. The result is the interval of the truth value, with `1` for true.\n *\n * @param x - The left side.\n * @param y - The right side.\n * @returns `[1, 1]` when `p <= q` for each pair of points, `[0, 0]` when for none, and `[0, 1]` when it is not known.\n */\nexport function le(x: Interval, y: Interval): Interval {\n if (x === null || y === null) return null;\n if (x[1] <= y[0]) return TRUE;\n if (x[0] > y[1]) return FALSE;\n return UNKNOWN;\n}\n\n/**\n * This function compares two intervals with `=`. The result is the interval of the truth value, with `1` for true.\n *\n * @param x - The left side.\n * @param y - The right side.\n * @returns `[1, 1]` when both are the same point, `[0, 0]` when they do not meet, and `[0, 1]` when it is not known.\n */\nexport function eq(x: Interval, y: Interval): Interval {\n if (x === null || y === null) return null;\n if (x[0] === x[1] && y[0] === y[1] && x[0] === y[0]) return TRUE;\n if (x[1] < y[0] || y[1] < x[0]) return FALSE;\n return UNKNOWN;\n}\n","/**\n * Directed rounding of nonnegative floats, from the exact error terms of `interval.ts`. Each function gives the\n * correctly rounded result when it is exact, and else the result one float step in the stated direction.\n *\n * @packageDocumentation\n */\nimport { nextDown, nextUp, productError, quotientIsExact, sumError } from \"./interval.ts\";\n\n/**\n * This function gives the sum of two nonnegative floats, rounded upward.\n *\n * @param x - The first value, 0 or more.\n * @param y - The second value, 0 or more.\n * @returns A float that is at least `x + y`, and at most one step above it.\n */\nexport function addUp(x: number, y: number): number {\n const s = x + y;\n return sumError(x, y, s) <= 0 ? s : nextUp(s);\n}\n\n/**\n * This function gives the product of two nonnegative floats, rounded upward.\n *\n * @param x - The first value, 0 or more.\n * @param y - The second value, 0 or more.\n * @returns A float that is at least `x * y`, and at most one step above it.\n */\nexport function mulUp(x: number, y: number): number {\n const p = x * y;\n return productError(x, y, p) <= 0 ? p : nextUp(p);\n}\n\n/**\n * This function gives the quotient of two nonnegative floats, rounded upward. A correctly rounded quotient is at most\n * one step low.\n *\n * @param x - The dividend, 0 or more.\n * @param y - The divisor, above 0.\n * @returns A float that is at least `x / y`, and at most one step above it.\n */\nexport function divUp(x: number, y: number): number {\n const q = x / y;\n return x === 0 || quotientIsExact(x, y, q) ? q : nextUp(q);\n}\n\n/**\n * This function gives the difference `x - y` of two nonnegative floats, rounded downward.\n *\n * @param x - The first value, 0 or more.\n * @param y - The value to subtract, 0 or more.\n * @returns A float that is at most `x - y`, and at most one step below it.\n */\nexport function subDown(x: number, y: number): number {\n const s = x - y;\n return sumError(x, -y, s) >= 0 ? s : nextDown(s);\n}\n\n/**\n * This function gives the float root of index `k`. The square root is correctly rounded. A larger index uses\n * `Math.cbrt` or `Math.pow`, which are not correctly rounded. An odd root of a negative value is negative.\n *\n * @param r - The radicand.\n * @param k - The index of the root, 2 or more.\n * @returns The float root.\n */\nexport function floatRoot(r: number, k: number): number {\n if (k === 2) return Math.sqrt(r);\n const f = (m: number): number => (k === 3 ? Math.cbrt(m) : Math.pow(m, 1 / k));\n return k % 2 === 1 && r < 0 ? -f(-r) : f(r);\n}\n","/**\n * Balls of real numbers: a float midpoint and a float radius, with the \"midrad\" arithmetic of Rump and of Arb. Each\n * function is a plain function of `{ mid, rad }` values. The midpoint of a result is the float result of the same\n * operation on the midpoints. The radius is the propagated radius of the parts plus the error of the rounding of the\n * midpoint. Each operation of the radius rounds upward.\n *\n * A finite radius is a claim. The exact result of the operation on each point of the input balls is at most the\n * radius from the midpoint. A radius of `Infinity` makes no claim. The radius rounds upward by one float step only\n * when its float operation is not exact, so an exact result has the radius zero.\n *\n * Proof sketch of the bound. Let the exact inputs of an operation be `X = a + dx` and `Y = b + dy`, with `|dx| <= ra`\n * and `|dy| <= rb`. Let `m` be the float result and `e` the error of its rounding.\n *\n * - A sum: `X + Y - m = e + dx + dy`, so `ra + rb + |e|` is a radius. `TwoSum` gives `e` exactly.\n * - A product: `XY - m = e + a dy + b dx + dx dy`, so `|a| rb + |b| ra + ra rb + |e|` is a radius. `TwoProduct`\n * gives `e` exactly away from overflow and underflow. Else `u |m| + 2^-1074` bounds `|e|`.\n * - A quotient, with `rb < |b|` so that `Y` is not zero: `X / Y - a / b = (b dx - a dy) / (b Y)`, and\n * `|b Y| >= |b| (|b| - rb)`. So `(ra + |a / b| rb) / (|b| - rb) + |e|` is a radius. Else the radius is `Infinity`.\n * - The smallest and the largest move at most by the largest radius of a part. `min` and `max` are 1-Lipschitz in\n * the largest distance. When the two balls do not meet, the extreme is one of them, so its radius is enough.\n * - A root goes through `interval.root` of `[m - r, m + r]`, rounded outward. The radius is the largest distance from\n * the midpoint to an end. An even root of a ball that holds a negative value has the radius `Infinity`.\n * - A comparison is decided when the outward intervals of the two balls decide it. The midpoint is then the truth\n * value and the radius is zero. Else the radius is one.\n *\n * The radius functions (`sumRadius`, `productRadius`, `quotientRadius`, `extremeRadius` and `rootRadius`) make no\n * object. An evaluator that keeps the midpoint and the radius in two numbers uses them.\n *\n * @packageDocumentation\n */\nimport * as I from \"./interval.ts\";\nimport type { Interval } from \"./interval.ts\";\nimport { addUp, divUp, floatRoot, mulUp, subDown } from \"./rounding.ts\";\n\n/** A ball: the reals at most `rad` from `mid`. */\nexport interface Ball {\n /** The midpoint. */\n readonly mid: number;\n /** The radius. `Infinity` makes no claim. */\n readonly rad: number;\n}\n\nconst U = 2 ** -53;\nconst MIN = Number.MIN_VALUE;\n\n/** The ball that makes no claim. */\nexport const NO_CLAIM: Ball = { mid: Number.NaN, rad: Infinity };\n\n/**\n * This function gives a bound of the error `|e|` of the rounding of a float result `m`. An exact error term `e`\n * gives `|e|`. An error term of `NaN` is not known, and it gives `u |m| + 2^-1074`, rounded upward.\n *\n * @param e - The exact error term, or `NaN`.\n * @param m - The float result.\n * @returns A bound of the error, rounded upward.\n */\nexport function roundingBound(e: number, m: number): number {\n return e === e ? Math.abs(e) : addUp(mulUp(U, Math.abs(m)), MIN);\n}\n\n/**\n * This function keeps a radius as a claim only when the midpoint is finite and the radius is a number.\n *\n * @param m - The midpoint.\n * @param r - The radius.\n * @returns `r`, or `Infinity` when `m` is not finite or `r` is `NaN`.\n */\nexport function claim(m: number, r: number): number {\n return m - m === 0 && r <= Infinity ? r : Infinity;\n}\n\n/**\n * This function makes a ball. A midpoint that is not finite gives the radius `Infinity`.\n *\n * @param mid - The midpoint.\n * @param rad - The radius, 0 or more. The default is 0.\n * @returns The ball.\n * @throws RangeError when the radius is negative or `NaN`.\n */\nexport function ball(mid: number, rad = 0): Ball {\n if (!(rad >= 0)) throw new RangeError(`The radius of a ball must be 0 or more, not ${rad}.`);\n return { mid, rad: claim(mid, rad) };\n}\n\n/**\n * This function gives the interval `[mid - rad, mid + rad]` of a ball, rounded outward.\n *\n * @param x - The ball.\n * @returns An interval that contains each point of the ball. A ball that makes no claim gives the whole line.\n */\nexport function toInterval(x: Ball): Interval {\n if (!(x.rad < Infinity)) return I.WHOLE;\n return I.add([x.mid, x.mid], [-x.rad, x.rad]);\n}\n\n/**\n * This function gives a ball that contains an interval. The midpoint is the float midpoint of the ends, and the\n * radius is the larger distance to an end, rounded upward.\n *\n * @param x - The interval.\n * @returns A ball that contains each point of `x`. The empty interval and an infinite end give `NO_CLAIM`.\n */\nexport function fromInterval(x: Interval): Ball {\n if (x === null || !(x[0] - x[0] === 0 && x[1] - x[1] === 0)) return NO_CLAIM;\n const [lo, hi] = x;\n const mid = lo / 2 + hi / 2;\n const d = I.sub([hi, hi], [mid, mid]) as readonly [number, number];\n const e = I.sub([mid, mid], [lo, lo]) as readonly [number, number];\n return { mid, rad: claim(mid, Math.max(d[1], e[1], 0)) };\n}\n\n/**\n * This function gives the radius of a sum `m = fl(a + b)`.\n *\n * @param m - The float sum of the midpoints.\n * @param a - The first midpoint.\n * @param ra - The first radius.\n * @param b - The second midpoint.\n * @param rb - The second radius.\n * @returns The radius of the sum, rounded upward.\n */\nexport function sumRadius(m: number, a: number, ra: number, b: number, rb: number): number {\n return claim(m, addUp(addUp(ra, rb), roundingBound(I.sumError(a, b, m), m)));\n}\n\n/**\n * This function gives the radius of a product `m = fl(a * b)`.\n *\n * @param m - The float product of the midpoints.\n * @param a - The first midpoint.\n * @param ra - The first radius.\n * @param b - The second midpoint.\n * @param rb - The second radius.\n * @returns The radius of the product, rounded upward.\n */\nexport function productRadius(m: number, a: number, ra: number, b: number, rb: number): number {\n const spread = addUp(addUp(mulUp(Math.abs(a), rb), mulUp(Math.abs(b), ra)), mulUp(ra, rb));\n return claim(m, addUp(spread, roundingBound(I.productError(a, b, m), m)));\n}\n\n/**\n * This function gives the radius of a quotient `q = fl(a / b)`.\n *\n * @param q - The float quotient of the midpoints.\n * @param a - The midpoint of the dividend.\n * @param ra - The radius of the dividend.\n * @param b - The midpoint of the divisor.\n * @param rb - The radius of the divisor.\n * @returns The radius of the quotient, rounded upward, or `Infinity` when the ball of the divisor can hold zero.\n */\nexport function quotientRadius(q: number, a: number, ra: number, b: number, rb: number): number {\n const m = Math.abs(b);\n if (!(rb < m) || !(q - q === 0)) return Infinity;\n const ratio = q === 0 ? 0 : I.nextUp(Math.abs(q));\n const spread = divUp(addUp(ra, mulUp(ratio, rb)), subDown(m, rb));\n const e = I.quotientIsExact(a, b, q) ? 0 : addUp(mulUp(U, Math.abs(q)), MIN);\n return claim(q, addUp(spread, e));\n}\n\n/**\n * This function gives the radius of the largest (`sign = 1`) or the smallest (`sign = -1`) of two balls. When the\n * outward intervals of the balls do not meet, the extreme is one ball, and its radius is the radius. Else the radius\n * is the larger radius.\n *\n * @param sign - `1` for the largest, `-1` for the smallest.\n * @param a - The first midpoint.\n * @param ra - The first radius.\n * @param b - The second midpoint.\n * @param rb - The second radius.\n * @returns The radius of the extreme. Give it to `claim` with the midpoint of the extreme.\n */\nexport function extremeRadius(sign: 1 | -1, a: number, ra: number, b: number, rb: number): number {\n const sa = I.add([sign * a, sign * a], [-ra, ra]) as readonly [number, number];\n const sb = I.add([sign * b, sign * b], [-rb, rb]) as readonly [number, number];\n if (sa[0] >= sb[1]) return ra;\n if (sb[0] >= sa[1]) return rb;\n return Math.max(ra, rb);\n}\n\n/**\n * This function gives the radius of a root `v = floatRoot(r, k)`. The radius is the largest distance from `v` to an\n * end of `interval.root` of the ball of the radicand, rounded upward.\n *\n * @param v - The float root of the midpoint.\n * @param r - The midpoint of the radicand.\n * @param rr - The radius of the radicand.\n * @param k - The index of the root, a whole number of 2 or more.\n * @returns The radius of the root, or `Infinity` when an even root can meet a negative value.\n */\nexport function rootRadius(v: number, r: number, rr: number, k: number): number {\n if (!(rr < Infinity) || !(v - v === 0)) return Infinity;\n const span = I.add([r, r], [-rr, rr]);\n if (span === null || (k % 2 === 0 && span[0] < 0)) return Infinity;\n const d = I.sub([v, v], I.root(span, k));\n return d === null ? Infinity : claim(v, Math.max(-d[0], d[1]));\n}\n\n/**\n * This function gives the sum of two balls.\n *\n * @param x - The first ball.\n * @param y - The second ball.\n * @returns A ball that contains each sum of a point of `x` and a point of `y`.\n */\nexport function add(x: Ball, y: Ball): Ball {\n const m = x.mid + y.mid;\n return { mid: m, rad: sumRadius(m, x.mid, x.rad, y.mid, y.rad) };\n}\n\n/**\n * This function gives the difference of two balls.\n *\n * @param x - The first ball.\n * @param y - The ball to subtract.\n * @returns A ball that contains each difference of a point of `x` and a point of `y`.\n */\nexport function sub(x: Ball, y: Ball): Ball {\n const m = x.mid - y.mid;\n return { mid: m, rad: sumRadius(m, x.mid, x.rad, -y.mid, y.rad) };\n}\n\n/**\n * This function gives the negative of a ball. It is exact.\n *\n * @param x - The ball.\n * @returns The ball of the negative points.\n */\nexport function neg(x: Ball): Ball {\n return { mid: -x.mid, rad: x.rad };\n}\n\n/**\n * This function gives the product of two balls.\n *\n * @param x - The first ball.\n * @param y - The second ball.\n * @returns A ball that contains each product of a point of `x` and a point of `y`.\n */\nexport function mul(x: Ball, y: Ball): Ball {\n const m = x.mid * y.mid;\n return { mid: m, rad: productRadius(m, x.mid, x.rad, y.mid, y.rad) };\n}\n\n/**\n * This function gives the quotient of two balls.\n *\n * @param x - The dividend.\n * @param y - The divisor.\n * @returns A ball that contains each quotient of a point of `x` and a point of `y`. A divisor that can hold zero gives\n * the radius `Infinity`.\n */\nexport function div(x: Ball, y: Ball): Ball {\n const q = x.mid / y.mid;\n return { mid: q, rad: quotientRadius(q, x.mid, x.rad, y.mid, y.rad) };\n}\n\n/**\n * This function gives the smallest of two balls.\n *\n * @param x - The first ball.\n * @param y - The second ball.\n * @returns A ball that contains `min(p, q)` for each point `p` of `x` and `q` of `y`.\n */\nexport function min(x: Ball, y: Ball): Ball {\n const m = Math.min(x.mid, y.mid);\n return { mid: m, rad: claim(m, extremeRadius(-1, x.mid, x.rad, y.mid, y.rad)) };\n}\n\n/**\n * This function gives the largest of two balls.\n *\n * @param x - The first ball.\n * @param y - The second ball.\n * @returns A ball that contains `max(p, q)` for each point `p` of `x` and `q` of `y`.\n */\nexport function max(x: Ball, y: Ball): Ball {\n const m = Math.max(x.mid, y.mid);\n return { mid: m, rad: claim(m, extremeRadius(1, x.mid, x.rad, y.mid, y.rad)) };\n}\n\n/**\n * This function gives a ball to a whole power, with repeated squares. Each product uses the rule of `mul`. A negative\n * power is the quotient of one by the positive power. The power zero gives one, with the radius zero when the base\n * makes a claim.\n *\n * @param x - The base.\n * @param n - The exponent, a whole number.\n * @returns A ball that contains the power of each point of `x`.\n */\nexport function pow(x: Ball, n: number): Ball {\n if (!Number.isInteger(n)) throw new RangeError(`pow needs a whole exponent, not ${n}.`);\n let bv = x.mid;\n let br = x.rad;\n if (n === 0) return { mid: 1, rad: br < Infinity ? 0 : Infinity };\n let rv = 1;\n let rr = 0;\n let first = true;\n for (let k = Math.abs(n); k > 0; k = Math.floor(k / 2)) {\n if (k % 2 === 1) {\n if (first) {\n rv = bv;\n rr = br;\n first = false;\n } else {\n const m = rv * bv;\n rr = productRadius(m, rv, rr, bv, br);\n rv = m;\n }\n }\n if (k > 1) {\n const s = bv * bv;\n br = productRadius(s, bv, br, bv, br);\n bv = s;\n }\n }\n if (n > 0) return { mid: rv, rad: rr };\n const v = 1 / rv;\n return { mid: v, rad: quotientRadius(v, 1, 0, rv, rr) };\n}\n\n/**\n * This function gives the root of index `k` of a ball.\n *\n * @param x - The radicand.\n * @param k - The index of the root, a whole number of 2 or more.\n * @returns A ball that contains the real root of each point of `x`. An even root of a ball that can hold a negative\n * value gives the radius `Infinity`.\n */\nexport function root(x: Ball, k: number): Ball {\n if (!Number.isInteger(k) || k < 2) throw new RangeError(`root needs a whole index of 2 or more, not ${k}.`);\n const v = floatRoot(x.mid, k);\n return { mid: v, rad: rootRadius(v, x.mid, x.rad, k) };\n}\n\n/** This function decides a comparison of two balls with the outward intervals of the balls. */\nfunction comparison(kind: \"lt\" | \"le\" | \"eq\", x: Ball, y: Ball): Ball {\n if (!(x.rad < Infinity && y.rad < Infinity)) return NO_CLAIM;\n const truth = I[kind](toInterval(x), toInterval(y)) as readonly [number, number];\n if (truth[0] === truth[1]) return { mid: truth[0], rad: 0 };\n const a = x.mid;\n const b = y.mid;\n return { mid: (kind === \"lt\" ? a < b : kind === \"le\" ? a <= b : a === b) ? 1 : 0, rad: 1 };\n}\n\n/**\n * This function compares two balls with `<`.\n *\n * @param x - The first ball.\n * @param y - The second ball.\n * @returns The truth value `1` or `0` with the radius zero when the balls decide it. Else the float truth value of\n * the midpoints with the radius one.\n */\nexport function lt(x: Ball, y: Ball): Ball {\n return comparison(\"lt\", x, y);\n}\n\n/**\n * This function compares two balls with `<=`.\n *\n * @param x - The first ball.\n * @param y - The second ball.\n * @returns The truth value with the rule of `lt`.\n */\nexport function le(x: Ball, y: Ball): Ball {\n return comparison(\"le\", x, y);\n}\n\n/**\n * This function compares two balls with `=`.\n *\n * @param x - The first ball.\n * @param y - The second ball.\n * @returns The truth value with the rule of `lt`.\n */\nexport function eq(x: Ball, y: Ball): Ball {\n return comparison(\"eq\", x, y);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAaA,MAAa,QAAmC,CAAC,WAAW,QAAQ;AAEpE,MAAM,uBAAO,IAAI,yBAAS,IAAI,YAAY,CAAC,CAAC;;;;;;;AAQ5C,SAAgB,OAAO,GAAmB;CACxC,IAAI,OAAO,MAAM,CAAC,KAAK,MAAM,UAAU,OAAO;CAC9C,IAAI,MAAM,GAAG,OAAO,OAAO;CAC3B,KAAK,WAAW,GAAG,CAAC;CACpB,MAAM,OAAO,KAAK,aAAa,CAAC;CAChC,KAAK,aAAa,GAAG,IAAI,IAAI,OAAO,KAAK,OAAO,EAAE;CAClD,OAAO,KAAK,WAAW,CAAC;AAC1B;;;;;;;AAQA,SAAgB,SAAS,GAAmB;CAC1C,OAAO,CAAC,OAAO,CAAC,CAAC;AACnB;;;;;;;;;AAUA,SAAgB,SAAS,GAAW,GAAW,GAAmB;CAChE,IAAI,CAAC,OAAO,SAAS,CAAC,GAAG,OAAO;CAChC,MAAM,KAAK,IAAI;CACf,OAAO,KAAK,IAAI,OAAO,IAAI;AAC7B;AAEA,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AAC,KAAK,IAAI,GAAG,MAAM,IAAI,WAAW,8CAA8C,EAAE,EAAE;CAC1G,MAAM,IAAI,UAAU,EAAE,KAAK,CAAC;CAC5B,OAAO;EAAE,KAAK;EAAG,KAAK,WAAW,GAAG,EAAE,KAAK,EAAE,KAAK,CAAC;CAAE;AACvD;;AAGA,SAAS,WAAW,MAA0B,GAAS,GAAe;CACpE,IAAI,EAAE,EAAE,MAAM,YAAY,EAAE,MAAM,WAAW,OAAO;CACpD,MAAM,QAAQC,iBAAE,KAAK,CAAC,WAAW,CAAC,GAAG,WAAW,CAAC,CAAC;CAClD,IAAI,MAAM,OAAO,MAAM,IAAI,OAAO;EAAE,KAAK,MAAM;EAAI,KAAK;CAAE;CAC1D,MAAM,IAAI,EAAE;CACZ,MAAM,IAAI,EAAE;CACZ,OAAO;EAAE,MAAM,SAAS,OAAO,IAAI,IAAI,SAAS,OAAO,KAAK,IAAI,MAAM,KAAK,IAAI;EAAG,KAAK;CAAE;AAC3F;;;;;;;;;AAUA,SAAgB,GAAG,GAAS,GAAe;CACzC,OAAO,WAAW,MAAM,GAAG,CAAC;AAC9B;;;;;;;;AASA,SAAgB,GAAG,GAAS,GAAe;CACzC,OAAO,WAAW,MAAM,GAAG,CAAC;AAC9B;;;;;;;;AASA,SAAgB,GAAG,GAAS,GAAe;CACzC,OAAO,WAAW,MAAM,GAAG,CAAC;AAC9B"}
@@ -0,0 +1,12 @@
1
+ // \0rolldown/runtime.js
2
+ var __defProp = Object.defineProperty;
3
+ var __exportAll = (all, flags) => {
4
+ let target = Object.setPrototypeOf({}, null);
5
+ for (var name in all) __defProp(target, name, {
6
+ get: all[name],
7
+ enumerable: true
8
+ });
9
+ if (!(flags & 1)) __defProp(target, Symbol.toStringTag, { value: "Module" });
10
+ return flags & 2 ? target : Object.preventExtensions(target);
11
+ };
12
+ export { __exportAll as t };
package/package.json CHANGED
@@ -1,13 +1,67 @@
1
1
  {
2
+ "$schema": "https://json.schemastore.org/package",
2
3
  "name": "@mark1russell7/interval",
3
- "version": "0.0.0-bootstrap.0",
4
- "description": "A placeholder that lets npm trust the release workflow of mark1russell7/algebra. Use a later version.",
4
+ "version": "0.1.0",
5
+ "description": "Float error bounds: outward-rounded intervals, midpoint-radius balls, nextUp and nextDown, and the error-free transforms of a sum and a product.",
6
+ "keywords": [
7
+ "interval-arithmetic",
8
+ "ball-arithmetic",
9
+ "midrad",
10
+ "error-bound",
11
+ "rounding",
12
+ "floating-point",
13
+ "twosum",
14
+ "twoproduct"
15
+ ],
16
+ "homepage": "https://github.com/mark1russell7/algebra/tree/main/packages/interval#readme",
17
+ "bugs": {
18
+ "url": "https://github.com/mark1russell7/algebra/issues"
19
+ },
5
20
  "repository": {
6
21
  "type": "git",
7
- "url": "git+https://github.com/mark1russell7/algebra.git"
22
+ "url": "git+https://github.com/mark1russell7/algebra.git",
23
+ "directory": "packages/interval"
8
24
  },
9
25
  "license": "MIT",
26
+ "author": "Mark Russell (https://github.com/mark1russell7)",
27
+ "type": "module",
28
+ "sideEffects": false,
29
+ "main": "./build/index.js",
30
+ "types": "./build/index.d.ts",
31
+ "exports": {
32
+ ".": {
33
+ "types": "./build/index.d.ts",
34
+ "default": "./build/index.js"
35
+ },
36
+ "./package.json": "./package.json"
37
+ },
38
+ "files": [
39
+ "build",
40
+ "src",
41
+ "!src/**/*.test.ts",
42
+ "!src/**/*.test-helper.ts"
43
+ ],
10
44
  "publishConfig": {
11
45
  "access": "public"
46
+ },
47
+ "engines": {
48
+ "node": ">=22"
49
+ },
50
+ "devDependencies": {
51
+ "@mark1russell7/cue": "^2.1.1",
52
+ "@mark1russell7/rational": "0.1.2",
53
+ "@mark1russell7/rigor-generators": "^0.1.8",
54
+ "@mark1russell7/rigor-statistics": "^0.2.1",
55
+ "@types/node": "^26.6.5",
56
+ "fast-check": "^4.10.2",
57
+ "tsdown": "^0.23.4",
58
+ "typescript": "^7.0.2",
59
+ "vitest": "^5.0.3"
60
+ },
61
+ "scripts": {
62
+ "build": "tsdown",
63
+ "test": "vitest run",
64
+ "test:watch": "vitest",
65
+ "typecheck": "tsc -p tsconfig.json && tsc -p tsconfig.test.json"
12
66
  }
13
- }
67
+ }
package/src/ball.ts ADDED
@@ -0,0 +1,377 @@
1
+ /**
2
+ * Balls of real numbers: a float midpoint and a float radius, with the "midrad" arithmetic of Rump and of Arb. Each
3
+ * function is a plain function of `{ mid, rad }` values. The midpoint of a result is the float result of the same
4
+ * operation on the midpoints. The radius is the propagated radius of the parts plus the error of the rounding of the
5
+ * midpoint. Each operation of the radius rounds upward.
6
+ *
7
+ * A finite radius is a claim. The exact result of the operation on each point of the input balls is at most the
8
+ * radius from the midpoint. A radius of `Infinity` makes no claim. The radius rounds upward by one float step only
9
+ * when its float operation is not exact, so an exact result has the radius zero.
10
+ *
11
+ * Proof sketch of the bound. Let the exact inputs of an operation be `X = a + dx` and `Y = b + dy`, with `|dx| <= ra`
12
+ * and `|dy| <= rb`. Let `m` be the float result and `e` the error of its rounding.
13
+ *
14
+ * - A sum: `X + Y - m = e + dx + dy`, so `ra + rb + |e|` is a radius. `TwoSum` gives `e` exactly.
15
+ * - A product: `XY - m = e + a dy + b dx + dx dy`, so `|a| rb + |b| ra + ra rb + |e|` is a radius. `TwoProduct`
16
+ * gives `e` exactly away from overflow and underflow. Else `u |m| + 2^-1074` bounds `|e|`.
17
+ * - A quotient, with `rb < |b|` so that `Y` is not zero: `X / Y - a / b = (b dx - a dy) / (b Y)`, and
18
+ * `|b Y| >= |b| (|b| - rb)`. So `(ra + |a / b| rb) / (|b| - rb) + |e|` is a radius. Else the radius is `Infinity`.
19
+ * - The smallest and the largest move at most by the largest radius of a part. `min` and `max` are 1-Lipschitz in
20
+ * the largest distance. When the two balls do not meet, the extreme is one of them, so its radius is enough.
21
+ * - A root goes through `interval.root` of `[m - r, m + r]`, rounded outward. The radius is the largest distance from
22
+ * the midpoint to an end. An even root of a ball that holds a negative value has the radius `Infinity`.
23
+ * - A comparison is decided when the outward intervals of the two balls decide it. The midpoint is then the truth
24
+ * value and the radius is zero. Else the radius is one.
25
+ *
26
+ * The radius functions (`sumRadius`, `productRadius`, `quotientRadius`, `extremeRadius` and `rootRadius`) make no
27
+ * object. An evaluator that keeps the midpoint and the radius in two numbers uses them.
28
+ *
29
+ * @packageDocumentation
30
+ */
31
+ import * as I from "./interval.ts";
32
+ import type { Interval } from "./interval.ts";
33
+ import { addUp, divUp, floatRoot, mulUp, subDown } from "./rounding.ts";
34
+
35
+ /** A ball: the reals at most `rad` from `mid`. */
36
+ export interface Ball {
37
+ /** The midpoint. */
38
+ readonly mid: number;
39
+ /** The radius. `Infinity` makes no claim. */
40
+ readonly rad: number;
41
+ }
42
+
43
+ const U = 2 ** -53;
44
+ const MIN = Number.MIN_VALUE;
45
+
46
+ /** The ball that makes no claim. */
47
+ export const NO_CLAIM: Ball = { mid: Number.NaN, rad: Infinity };
48
+
49
+ /**
50
+ * This function gives a bound of the error `|e|` of the rounding of a float result `m`. An exact error term `e`
51
+ * gives `|e|`. An error term of `NaN` is not known, and it gives `u |m| + 2^-1074`, rounded upward.
52
+ *
53
+ * @param e - The exact error term, or `NaN`.
54
+ * @param m - The float result.
55
+ * @returns A bound of the error, rounded upward.
56
+ */
57
+ export function roundingBound(e: number, m: number): number {
58
+ return e === e ? Math.abs(e) : addUp(mulUp(U, Math.abs(m)), MIN);
59
+ }
60
+
61
+ /**
62
+ * This function keeps a radius as a claim only when the midpoint is finite and the radius is a number.
63
+ *
64
+ * @param m - The midpoint.
65
+ * @param r - The radius.
66
+ * @returns `r`, or `Infinity` when `m` is not finite or `r` is `NaN`.
67
+ */
68
+ export function claim(m: number, r: number): number {
69
+ return m - m === 0 && r <= Infinity ? r : Infinity;
70
+ }
71
+
72
+ /**
73
+ * This function makes a ball. A midpoint that is not finite gives the radius `Infinity`.
74
+ *
75
+ * @param mid - The midpoint.
76
+ * @param rad - The radius, 0 or more. The default is 0.
77
+ * @returns The ball.
78
+ * @throws RangeError when the radius is negative or `NaN`.
79
+ */
80
+ export function ball(mid: number, rad = 0): Ball {
81
+ if (!(rad >= 0)) throw new RangeError(`The radius of a ball must be 0 or more, not ${rad}.`);
82
+ return { mid, rad: claim(mid, rad) };
83
+ }
84
+
85
+ /**
86
+ * This function gives the interval `[mid - rad, mid + rad]` of a ball, rounded outward.
87
+ *
88
+ * @param x - The ball.
89
+ * @returns An interval that contains each point of the ball. A ball that makes no claim gives the whole line.
90
+ */
91
+ export function toInterval(x: Ball): Interval {
92
+ if (!(x.rad < Infinity)) return I.WHOLE;
93
+ return I.add([x.mid, x.mid], [-x.rad, x.rad]);
94
+ }
95
+
96
+ /**
97
+ * This function gives a ball that contains an interval. The midpoint is the float midpoint of the ends, and the
98
+ * radius is the larger distance to an end, rounded upward.
99
+ *
100
+ * @param x - The interval.
101
+ * @returns A ball that contains each point of `x`. The empty interval and an infinite end give `NO_CLAIM`.
102
+ */
103
+ export function fromInterval(x: Interval): Ball {
104
+ if (x === null || !(x[0] - x[0] === 0 && x[1] - x[1] === 0)) return NO_CLAIM;
105
+ const [lo, hi] = x;
106
+ const mid = lo / 2 + hi / 2;
107
+ const d = I.sub([hi, hi], [mid, mid]) as readonly [number, number];
108
+ const e = I.sub([mid, mid], [lo, lo]) as readonly [number, number];
109
+ return { mid, rad: claim(mid, Math.max(d[1], e[1], 0)) };
110
+ }
111
+
112
+ /**
113
+ * This function gives the radius of a sum `m = fl(a + b)`.
114
+ *
115
+ * @param m - The float sum of the midpoints.
116
+ * @param a - The first midpoint.
117
+ * @param ra - The first radius.
118
+ * @param b - The second midpoint.
119
+ * @param rb - The second radius.
120
+ * @returns The radius of the sum, rounded upward.
121
+ */
122
+ export function sumRadius(m: number, a: number, ra: number, b: number, rb: number): number {
123
+ return claim(m, addUp(addUp(ra, rb), roundingBound(I.sumError(a, b, m), m)));
124
+ }
125
+
126
+ /**
127
+ * This function gives the radius of a product `m = fl(a * b)`.
128
+ *
129
+ * @param m - The float product of the midpoints.
130
+ * @param a - The first midpoint.
131
+ * @param ra - The first radius.
132
+ * @param b - The second midpoint.
133
+ * @param rb - The second radius.
134
+ * @returns The radius of the product, rounded upward.
135
+ */
136
+ export function productRadius(m: number, a: number, ra: number, b: number, rb: number): number {
137
+ const spread = addUp(addUp(mulUp(Math.abs(a), rb), mulUp(Math.abs(b), ra)), mulUp(ra, rb));
138
+ return claim(m, addUp(spread, roundingBound(I.productError(a, b, m), m)));
139
+ }
140
+
141
+ /**
142
+ * This function gives the radius of a quotient `q = fl(a / b)`.
143
+ *
144
+ * @param q - The float quotient of the midpoints.
145
+ * @param a - The midpoint of the dividend.
146
+ * @param ra - The radius of the dividend.
147
+ * @param b - The midpoint of the divisor.
148
+ * @param rb - The radius of the divisor.
149
+ * @returns The radius of the quotient, rounded upward, or `Infinity` when the ball of the divisor can hold zero.
150
+ */
151
+ export function quotientRadius(q: number, a: number, ra: number, b: number, rb: number): number {
152
+ const m = Math.abs(b);
153
+ if (!(rb < m) || !(q - q === 0)) return Infinity;
154
+ const ratio = q === 0 ? 0 : I.nextUp(Math.abs(q));
155
+ const spread = divUp(addUp(ra, mulUp(ratio, rb)), subDown(m, rb));
156
+ const e = I.quotientIsExact(a, b, q) ? 0 : addUp(mulUp(U, Math.abs(q)), MIN);
157
+ return claim(q, addUp(spread, e));
158
+ }
159
+
160
+ /**
161
+ * This function gives the radius of the largest (`sign = 1`) or the smallest (`sign = -1`) of two balls. When the
162
+ * outward intervals of the balls do not meet, the extreme is one ball, and its radius is the radius. Else the radius
163
+ * is the larger radius.
164
+ *
165
+ * @param sign - `1` for the largest, `-1` for the smallest.
166
+ * @param a - The first midpoint.
167
+ * @param ra - The first radius.
168
+ * @param b - The second midpoint.
169
+ * @param rb - The second radius.
170
+ * @returns The radius of the extreme. Give it to `claim` with the midpoint of the extreme.
171
+ */
172
+ export function extremeRadius(sign: 1 | -1, a: number, ra: number, b: number, rb: number): number {
173
+ const sa = I.add([sign * a, sign * a], [-ra, ra]) as readonly [number, number];
174
+ const sb = I.add([sign * b, sign * b], [-rb, rb]) as readonly [number, number];
175
+ if (sa[0] >= sb[1]) return ra;
176
+ if (sb[0] >= sa[1]) return rb;
177
+ return Math.max(ra, rb);
178
+ }
179
+
180
+ /**
181
+ * This function gives the radius of a root `v = floatRoot(r, k)`. The radius is the largest distance from `v` to an
182
+ * end of `interval.root` of the ball of the radicand, rounded upward.
183
+ *
184
+ * @param v - The float root of the midpoint.
185
+ * @param r - The midpoint of the radicand.
186
+ * @param rr - The radius of the radicand.
187
+ * @param k - The index of the root, a whole number of 2 or more.
188
+ * @returns The radius of the root, or `Infinity` when an even root can meet a negative value.
189
+ */
190
+ export function rootRadius(v: number, r: number, rr: number, k: number): number {
191
+ if (!(rr < Infinity) || !(v - v === 0)) return Infinity;
192
+ const span = I.add([r, r], [-rr, rr]);
193
+ if (span === null || (k % 2 === 0 && span[0] < 0)) return Infinity;
194
+ const d = I.sub([v, v], I.root(span, k));
195
+ return d === null ? Infinity : claim(v, Math.max(-d[0], d[1]));
196
+ }
197
+
198
+ /**
199
+ * This function gives the sum of two balls.
200
+ *
201
+ * @param x - The first ball.
202
+ * @param y - The second ball.
203
+ * @returns A ball that contains each sum of a point of `x` and a point of `y`.
204
+ */
205
+ export function add(x: Ball, y: Ball): Ball {
206
+ const m = x.mid + y.mid;
207
+ return { mid: m, rad: sumRadius(m, x.mid, x.rad, y.mid, y.rad) };
208
+ }
209
+
210
+ /**
211
+ * This function gives the difference of two balls.
212
+ *
213
+ * @param x - The first ball.
214
+ * @param y - The ball to subtract.
215
+ * @returns A ball that contains each difference of a point of `x` and a point of `y`.
216
+ */
217
+ export function sub(x: Ball, y: Ball): Ball {
218
+ const m = x.mid - y.mid;
219
+ return { mid: m, rad: sumRadius(m, x.mid, x.rad, -y.mid, y.rad) };
220
+ }
221
+
222
+ /**
223
+ * This function gives the negative of a ball. It is exact.
224
+ *
225
+ * @param x - The ball.
226
+ * @returns The ball of the negative points.
227
+ */
228
+ export function neg(x: Ball): Ball {
229
+ return { mid: -x.mid, rad: x.rad };
230
+ }
231
+
232
+ /**
233
+ * This function gives the product of two balls.
234
+ *
235
+ * @param x - The first ball.
236
+ * @param y - The second ball.
237
+ * @returns A ball that contains each product of a point of `x` and a point of `y`.
238
+ */
239
+ export function mul(x: Ball, y: Ball): Ball {
240
+ const m = x.mid * y.mid;
241
+ return { mid: m, rad: productRadius(m, x.mid, x.rad, y.mid, y.rad) };
242
+ }
243
+
244
+ /**
245
+ * This function gives the quotient of two balls.
246
+ *
247
+ * @param x - The dividend.
248
+ * @param y - The divisor.
249
+ * @returns A ball that contains each quotient of a point of `x` and a point of `y`. A divisor that can hold zero gives
250
+ * the radius `Infinity`.
251
+ */
252
+ export function div(x: Ball, y: Ball): Ball {
253
+ const q = x.mid / y.mid;
254
+ return { mid: q, rad: quotientRadius(q, x.mid, x.rad, y.mid, y.rad) };
255
+ }
256
+
257
+ /**
258
+ * This function gives the smallest of two balls.
259
+ *
260
+ * @param x - The first ball.
261
+ * @param y - The second ball.
262
+ * @returns A ball that contains `min(p, q)` for each point `p` of `x` and `q` of `y`.
263
+ */
264
+ export function min(x: Ball, y: Ball): Ball {
265
+ const m = Math.min(x.mid, y.mid);
266
+ return { mid: m, rad: claim(m, extremeRadius(-1, x.mid, x.rad, y.mid, y.rad)) };
267
+ }
268
+
269
+ /**
270
+ * This function gives the largest of two balls.
271
+ *
272
+ * @param x - The first ball.
273
+ * @param y - The second ball.
274
+ * @returns A ball that contains `max(p, q)` for each point `p` of `x` and `q` of `y`.
275
+ */
276
+ export function max(x: Ball, y: Ball): Ball {
277
+ const m = Math.max(x.mid, y.mid);
278
+ return { mid: m, rad: claim(m, extremeRadius(1, x.mid, x.rad, y.mid, y.rad)) };
279
+ }
280
+
281
+ /**
282
+ * This function gives a ball to a whole power, with repeated squares. Each product uses the rule of `mul`. A negative
283
+ * power is the quotient of one by the positive power. The power zero gives one, with the radius zero when the base
284
+ * makes a claim.
285
+ *
286
+ * @param x - The base.
287
+ * @param n - The exponent, a whole number.
288
+ * @returns A ball that contains the power of each point of `x`.
289
+ */
290
+ export function pow(x: Ball, n: number): Ball {
291
+ if (!Number.isInteger(n)) throw new RangeError(`pow needs a whole exponent, not ${n}.`);
292
+ let bv = x.mid;
293
+ let br = x.rad;
294
+ if (n === 0) return { mid: 1, rad: br < Infinity ? 0 : Infinity };
295
+ let rv = 1;
296
+ let rr = 0;
297
+ let first = true;
298
+ for (let k = Math.abs(n); k > 0; k = Math.floor(k / 2)) {
299
+ if (k % 2 === 1) {
300
+ if (first) {
301
+ rv = bv;
302
+ rr = br;
303
+ first = false;
304
+ } else {
305
+ const m = rv * bv;
306
+ rr = productRadius(m, rv, rr, bv, br);
307
+ rv = m;
308
+ }
309
+ }
310
+ if (k > 1) {
311
+ const s = bv * bv;
312
+ br = productRadius(s, bv, br, bv, br);
313
+ bv = s;
314
+ }
315
+ }
316
+ if (n > 0) return { mid: rv, rad: rr };
317
+ const v = 1 / rv;
318
+ return { mid: v, rad: quotientRadius(v, 1, 0, rv, rr) };
319
+ }
320
+
321
+ /**
322
+ * This function gives the root of index `k` of a ball.
323
+ *
324
+ * @param x - The radicand.
325
+ * @param k - The index of the root, a whole number of 2 or more.
326
+ * @returns A ball that contains the real root of each point of `x`. An even root of a ball that can hold a negative
327
+ * value gives the radius `Infinity`.
328
+ */
329
+ export function root(x: Ball, k: number): Ball {
330
+ if (!Number.isInteger(k) || k < 2) throw new RangeError(`root needs a whole index of 2 or more, not ${k}.`);
331
+ const v = floatRoot(x.mid, k);
332
+ return { mid: v, rad: rootRadius(v, x.mid, x.rad, k) };
333
+ }
334
+
335
+ /** This function decides a comparison of two balls with the outward intervals of the balls. */
336
+ function comparison(kind: "lt" | "le" | "eq", x: Ball, y: Ball): Ball {
337
+ if (!(x.rad < Infinity && y.rad < Infinity)) return NO_CLAIM;
338
+ const truth = I[kind](toInterval(x), toInterval(y)) as readonly [number, number];
339
+ if (truth[0] === truth[1]) return { mid: truth[0], rad: 0 };
340
+ const a = x.mid;
341
+ const b = y.mid;
342
+ return { mid: (kind === "lt" ? a < b : kind === "le" ? a <= b : a === b) ? 1 : 0, rad: 1 };
343
+ }
344
+
345
+ /**
346
+ * This function compares two balls with `<`.
347
+ *
348
+ * @param x - The first ball.
349
+ * @param y - The second ball.
350
+ * @returns The truth value `1` or `0` with the radius zero when the balls decide it. Else the float truth value of
351
+ * the midpoints with the radius one.
352
+ */
353
+ export function lt(x: Ball, y: Ball): Ball {
354
+ return comparison("lt", x, y);
355
+ }
356
+
357
+ /**
358
+ * This function compares two balls with `<=`.
359
+ *
360
+ * @param x - The first ball.
361
+ * @param y - The second ball.
362
+ * @returns The truth value with the rule of `lt`.
363
+ */
364
+ export function le(x: Ball, y: Ball): Ball {
365
+ return comparison("le", x, y);
366
+ }
367
+
368
+ /**
369
+ * This function compares two balls with `=`.
370
+ *
371
+ * @param x - The first ball.
372
+ * @param y - The second ball.
373
+ * @returns The truth value with the rule of `lt`.
374
+ */
375
+ export function eq(x: Ball, y: Ball): Ball {
376
+ return comparison("eq", x, y);
377
+ }
package/src/index.ts ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Float error bounds, with no imports. The namespace `interval` has the outward-rounded intervals, and the namespace
3
+ * `ball` has the midpoint-radius balls. The steps of a float, the error-free transforms of a sum and a product, and
4
+ * the directed rounding are also at the top level.
5
+ *
6
+ * @packageDocumentation
7
+ */
8
+ export * as ball from "./ball.ts";
9
+ export type { Ball } from "./ball.ts";
10
+ export * as interval from "./interval.ts";
11
+ export { nextDown, nextUp, productError, quotientIsExact, sumError, WHOLE } from "./interval.ts";
12
+ export type { Interval } from "./interval.ts";
13
+ export { addUp, divUp, floatRoot, mulUp, subDown } from "./rounding.ts";