fix NaN equality for trueEquals for compound types

This commit is contained in:
Joe 2026-02-19 02:02:10 -06:00
parent 354d4c8b21
commit b105d86541
2 changed files with 30 additions and 3 deletions

View file

@ -13,6 +13,12 @@ local NULL_REF: string = "00000000000000000000000000000000"
local EPSILON: number = 0.0001 local EPSILON: number = 0.0001
local function fuzzyEq(a: number, b: number): boolean local function fuzzyEq(a: number, b: number): boolean
if a == b then
return true
end
if a ~= a then
return b ~= b
end
local diff = math.abs(a - b) local diff = math.abs(a - b)
local maxVal = math.max(math.abs(a), math.abs(b), 1) local maxVal = math.max(math.abs(a), math.abs(b), 1)
return diff < EPSILON or diff < maxVal * EPSILON return diff < EPSILON or diff < maxVal * EPSILON
@ -90,9 +96,6 @@ local function trueEquals(a: any, b: any): boolean
end end
if t == "number" then if t == "number" then
if a ~= a then
return b ~= b
end
return fuzzyEq(a, b) return fuzzyEq(a, b)
end end
if t == "Color3" then if t == "Color3" then

View file

@ -224,6 +224,14 @@ return function()
it("large values within relative epsilon", function() it("large values within relative epsilon", function()
expect(trueEquals(Vector3.new(1e6, 1e6, 1e6), Vector3.new(1e6 + 50, 1e6, 1e6))).to.equal(true) expect(trueEquals(Vector3.new(1e6, 1e6, 1e6), Vector3.new(1e6 + 50, 1e6, 1e6))).to.equal(true)
end) end)
it("NaN component equal", function()
expect(trueEquals(Vector3.new(0 / 0, 1, 2), Vector3.new(0 / 0, 1, 2))).to.equal(true)
end)
it("NaN vs finite component", function()
expect(trueEquals(Vector3.new(0 / 0, 1, 2), Vector3.new(0, 1, 2))).to.equal(false)
end)
end) end)
describe("Vector2", function() describe("Vector2", function()
@ -238,6 +246,22 @@ return function()
it("Y differs by 1", function() it("Y differs by 1", function()
expect(trueEquals(Vector2.new(1, 2), Vector2.new(1, 3))).to.equal(false) expect(trueEquals(Vector2.new(1, 2), Vector2.new(1, 3))).to.equal(false)
end) end)
it("infinity components", function()
expect(trueEquals(Vector2.new(math.huge, math.huge), Vector2.new(math.huge, math.huge))).to.equal(true)
end)
it("mixed infinity and finite", function()
expect(trueEquals(Vector2.new(math.huge, 0), Vector2.new(math.huge, 1))).to.equal(false)
end)
it("NaN components", function()
expect(trueEquals(Vector2.new(0 / 0, 0 / 0), Vector2.new(0 / 0, 0 / 0))).to.equal(true)
end)
it("NaN vs finite component", function()
expect(trueEquals(Vector2.new(0 / 0, 1), Vector2.new(1, 1))).to.equal(false)
end)
end) end)
describe("CFrame", function() describe("CFrame", function()