From ac55c48f0c406b278df93eafd99f1aada1435bba Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Sat, 19 Sep 2026 02:17:54 +0000 Subject: [PATCH 1/3] Make Num.equals threshold inclusive Num.equals compared with a strict <, so a number was not equal to itself at threshold 0 and a difference exactly at the threshold was reported as unequal. Pt.equals, the sibling comparison, already uses an inclusive <=. Match it. --- src/Num.ts | 4 +++- src/test/Num.spec.ts | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Num.ts b/src/Num.ts index d853550d..684e92f0 100644 --- a/src/Num.ts +++ b/src/Num.ts @@ -26,7 +26,9 @@ export class Num { * @param threshold threshold value that specifies the minimum difference within which the two numbers are considered equal */ static equals(a: number, b: number, threshold = 0.00001): boolean { - return Math.abs(a - b) < threshold; + // inclusive, like `Pt.equals`: a number equals itself at threshold 0, and a + // difference exactly at the threshold still counts as equal + return Math.abs(a - b) <= threshold; } /** diff --git a/src/test/Num.spec.ts b/src/test/Num.spec.ts index 7690823f..e149f195 100644 --- a/src/test/Num.spec.ts +++ b/src/test/Num.spec.ts @@ -31,6 +31,24 @@ describe("Num", () => { expect(Num.within(6, 5, 1)).toBe(false); }); + it("treats the equality threshold as inclusive, like Pt.equals", () => { + // a number equals itself at any threshold, including 0 + expect(Num.equals(5, 5, 0)).toBe(true); + expect(Num.equals(0, 0, 0)).toBe(true); + expect(Num.equals(-3.5, -3.5, 0)).toBe(true); + // a difference exactly at the threshold counts as equal + expect(Num.equals(1, 1.5, 0.5)).toBe(true); + expect(Num.equals(0, 0.25, 0.25)).toBe(true); + // Pt.equals, the sibling comparison, already answers the same way + expect(new Pt([5, 5]).equals([5, 5], 0)).toBe(true); + expect(new Pt([1, 1]).equals([1.5, 1], 0.5)).toBe(true); + // beyond the threshold is still unequal, and NaN is never equal + expect(Num.equals(1, 2, 0.5)).toBe(false); + expect(Num.equals(1, 1.5000001, 0.5)).toBe(false); + expect(Num.equals(NaN, NaN, 1)).toBe(false); + expect(Num.equals(1, 1, -1)).toBe(false); + }); + it("creates deterministic random ranges and points", () => { vi.spyOn(Num, "random").mockReturnValue(0.25); expect(Num.randomRange(10)).toBe(2.5); From 39b9601150929a67fb568daf8928d5a35e1c7809 Mon Sep 17 00:00:00 2001 From: askalf Date: Fri, 18 Sep 2026 22:41:24 -0400 Subject: [PATCH 2/3] Say what the inclusive threshold covers --- src/Num.ts | 3 +-- src/test/Num.spec.ts | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Num.ts b/src/Num.ts index 684e92f0..a712e8b6 100644 --- a/src/Num.ts +++ b/src/Num.ts @@ -26,8 +26,7 @@ export class Num { * @param threshold threshold value that specifies the minimum difference within which the two numbers are considered equal */ static equals(a: number, b: number, threshold = 0.00001): boolean { - // inclusive, like `Pt.equals`: a number equals itself at threshold 0, and a - // difference exactly at the threshold still counts as equal + // Finite differences equal to the threshold are included. return Math.abs(a - b) <= threshold; } diff --git a/src/test/Num.spec.ts b/src/test/Num.spec.ts index e149f195..0494ffb9 100644 --- a/src/test/Num.spec.ts +++ b/src/test/Num.spec.ts @@ -32,7 +32,7 @@ describe("Num", () => { }); it("treats the equality threshold as inclusive, like Pt.equals", () => { - // a number equals itself at any threshold, including 0 + // identical finite values compare equal at threshold 0 expect(Num.equals(5, 5, 0)).toBe(true); expect(Num.equals(0, 0, 0)).toBe(true); expect(Num.equals(-3.5, -3.5, 0)).toBe(true); From 667f2fcc5d2399a75b39525237a8fa3910d7dc0e Mon Sep 17 00:00:00 2001 From: askalf <263217947+askalf@users.noreply.github.com> Date: Sat, 19 Sep 2026 03:31:54 +0000 Subject: [PATCH 3/3] Cover the inclusive threshold boundaries Pin the default threshold at its exact limit, operand order, both spellings of zero, an infinite threshold and large magnitudes. --- src/test/Num.spec.ts | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/test/Num.spec.ts b/src/test/Num.spec.ts index 0494ffb9..ff27a4c6 100644 --- a/src/test/Num.spec.ts +++ b/src/test/Num.spec.ts @@ -49,6 +49,53 @@ describe("Num", () => { expect(Num.equals(1, 1, -1)).toBe(false); }); + it("treats the default threshold as inclusive at its exact limit", () => { + // a difference of exactly the documented default, 0.00001, is representable + expect(Math.abs(0 - 0.00001)).toBe(0.00001); + expect(Num.equals(0, 0.00001)).toBe(true); + expect(Num.equals(-0.00001, 0)).toBe(true); + // one representable step past the default is still unequal (control) + expect(Num.equals(0, 0.0000100000001)).toBe(false); + }); + + it("compares the same pair either way round", () => { + // the difference is symmetric, so swapping the operands cannot change the answer + expect(Num.equals(1.5, 1, 0.5)).toBe(true); + expect(Num.equals(0.25, 0, 0.25)).toBe(true); + expect(Num.equals(0.00001, 0)).toBe(true); + expect(Num.equals(1.5, 1, 0.5)).toBe(Num.equals(1, 1.5, 0.5)); + // and the beyond-threshold answer is symmetric too (control) + expect(Num.equals(2, 1, 0.5)).toBe(false); + }); + + it("treats both spellings of zero as the same threshold and the same value", () => { + // -0 is the falsy but valid zero, and must behave exactly like 0 + expect(Num.equals(5, 5, -0)).toBe(true); + expect(Num.equals(0, -0, 0)).toBe(true); + expect(Num.equals(-0, 0, -0)).toBe(true); + // the smallest representable difference is still a difference (control) + expect(Num.equals(5e-324, 0, 0)).toBe(false); + }); + + it("accepts an infinite threshold and keeps same-signed infinities apart", () => { + // an infinite tolerance means every measurable difference is equal + expect(Num.equals(Infinity, 1, Infinity)).toBe(true); + expect(Num.equals(Infinity, -Infinity, Infinity)).toBe(true); + // Infinity minus Infinity is NaN, so equal infinities never compare equal (control) + expect(Num.equals(Infinity, Infinity, 0)).toBe(false); + // a NaN threshold rejects, as every NaN comparison does (control) + expect(Num.equals(1, 1, NaN)).toBe(false); + // a finite pair under an infinite tolerance was already equal (control) + expect(Num.equals(1, 2, Infinity)).toBe(true); + }); + + it("holds at the boundary for large magnitudes", () => { + expect(Num.equals(Number.MAX_VALUE, Number.MAX_VALUE, 0)).toBe(true); + expect(Num.equals(1e16, 1e16 + 2, 2)).toBe(true); + // past the boundary at the same magnitude is still unequal (control) + expect(Num.equals(1e16, 1e16 + 4, 2)).toBe(false); + }); + it("creates deterministic random ranges and points", () => { vi.spyOn(Num, "random").mockReturnValue(0.25); expect(Num.randomRange(10)).toBe(2.5);